Hello,
Using C# 4.5,I'm trying to programmatically log-on to a third-party website and then download a csv file from another page on that site. I tried using following approach.I dont get any errors but it seems to download the contents of their logon page itself and save it as a csv file instead of the actual csv file having data.
Also,if I read the response stream after logging in, I see that I'm getting the log-on webpage back.
What am I missing here please? Plus, I need to post the name of the form as well.How do I achieve that?Please advise.
Thanks.
Here's my C# code:
Using C# 4.5,I'm trying to programmatically log-on to a third-party website and then download a csv file from another page on that site. I tried using following approach.I dont get any errors but it seems to download the contents of their logon page itself and save it as a csv file instead of the actual csv file having data.
Also,if I read the response stream after logging in, I see that I'm getting the log-on webpage back.
What am I missing here please? Plus, I need to post the name of the form as well.How do I achieve that?Please advise.
Thanks.
Here's my C# code:
string websiteURL = "https://testLoginPage"; string poststring = "user=testUser&password=TestPwd"; HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(websiteURL); httpRequest.Method = "POST"; httpRequest.ContentType = "application/x-www-form-urlencoded"; byte[] bytedata = Encoding.UTF8.GetBytes(poststring); httpRequest.ContentLength = bytedata.Length; //Log on to the site Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(bytedata, 0, bytedata.Length); requestStream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); string header1 = httpWebResponse.Headers.Get("Header1"); string header2 = httpWebResponse.Headers.Get("Header2"); string cookie = httpWebResponse.Headers.Get("Set-Cookie"); //Read the response StringBuilder sb = new StringBuilder(); using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { sb.Append(line); } } string htmlResponse = sb.ToString(); //Download the csv file // string localFileFormat = string.Format("{0}-{1}", "MyFile", DateTime.Now.ToString("yyyyMMdd")); string downloadedFilePath = string.Format(@"{0}\{1}.{2}", DownloadFileInputParams.OuputDirectory, localFileFormat, DownloadFileInputParams.FileType); string fileDownloadURL = "https://fileDownloadURL/data?fileType=csv"; httpRequest = (HttpWebRequest)WebRequest.Create(fileDownloadURL); httpRequest.CookieContainer = new CookieContainer(); httpRequest.Headers.Add("X-DB-NAR", header1); httpRequest.Headers.Add("DB-Nickname", header2); httpRequest.Headers.Add("Set-Cookie", cookie); httpWebResponse = (HttpWebResponse)httpRequest.GetResponse(); cookie = httpWebResponse.Headers.Get("Set-Cookie"); httpRequest = (HttpWebRequest)WebRequest.Create(fileDownloadURL); httpWebResponse = (HttpWebResponse)httpRequest.GetResponse(); responseStream = httpWebResponse.GetResponseStream(); FileStream localStream = File.Create(downloadedFilePath); byte[] buffer = new byte[1024]; int bytesRead; int bytesProcessed = 0; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = responseStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; } while (bytesRead > 0); if (httpWebResponse != null) httpWebResponse.Close(); if (responseStream != null) responseStream.Close(); if (localStream != null) localStream.Close(); return;
Last edited by a moderator: