Hello everybody,
I am struggling with this issue for a few days now. I need to create an
automatic process to daily download a file from a server that requires a
digital certificate. The protocol used is HTTPS. This is what I did so far:
// obtain a collection of certificates
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
collection = (X509Certificate2Collection)store.Certificates;
fcollection =
(X509Certificate2Collection)collection.Find(X509Fi ndType.FindByTimeValid,
DateTime.Now, false);
// URL for my file to download
string myFile2Get = "https://www.MySecureWebsite.com/MyFile.txt";
// create the specialized Web Request object
HttpWebRequest objHttpWebReq = WebRequest.Create(myFile2Get) as
HttpWebRequest;
// add the collection of the certificates
objHttpWebReq.ClientCertificates = fcollection;
// default method is GET
// get the response to my request
HttpWebResponse response = objHttpWebReq.GetResponse() as HttpWebResponse;
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader
// with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
Console.WriteLine("HTML...\r\n");
while (count 0)
{
// Dumps the 256 characters on a string and displays the string to the
console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
response.Close();
// Releases the resources of the Stream.
readStream.Close();
There is no error thrown, but instead of getting the content of the text
file I am trying to read, all I get is message saying "Virtual user
'NameOfMyCertificate' is logged in". What am I doing wrong? How do I get to
read the file and not just some status message? Is this something that is
done in two steps: connect to the secure site and then request the file? If
so, what would be the sequence?
Any help will be highly appreciated.
All the best,
Eddie