C # download a file from the Internet using login

I can already access the redirected webpage (I save cookies) with this code

CookieCollection cookies = new CookieCollection(); HttpWebRequest cookieRequest = (HttpWebRequest)WebRequest.Create("https://www.loginpage.com/"); cookieRequest.CookieContainer = new CookieContainer(); cookieRequest.CookieContainer.Add(cookies); HttpWebResponse cookieResponse = (HttpWebResponse)cookieRequest.GetResponse(); cookies = cookieResponse.Cookies; string postData = "name=********&password=*********&submit=submit"; HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("https://www.loginpage.com/"); loginRequest.CookieContainer = new CookieContainer(); loginRequest.CookieContainer.Add(cookies); loginRequest.Method = WebRequestMethods.Http.Post; loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; loginRequest.AllowWriteStreamBuffering = true; loginRequest.ProtocolVersion = HttpVersion.Version11; loginRequest.AllowAutoRedirect = true; loginRequest.ContentType = "application/x-www-form-urlencoded"; byte[] byteArray = Encoding.ASCII.GetBytes(postData); loginRequest.ContentLength = byteArray.Length; Stream newStream = loginRequest.GetRequestStream(); //open connection newStream.Write(byteArray, 0, byteArray.Length); // Send the data. newStream.Close(); 

This works fine, but I need to download the .xls file from here, it is here (for example)

 https://www.loginpage.com/export_excel.php?export_type=list 

for this i tried this code

  HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("https://www.loginpage.com/export_excel.php?export_type=list"); HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); Stream str = ws.GetResponseStream(); byte[] inBuf = new byte[100000]; int bytesReadTotal = 0; string path = @"d:\test.xlsx"; FileStream fstr = new FileStream(path, FileMode.Create, FileAccess.Write); while (true) { int n = str.Read(inBuf, 0, 100000); if ((n == 0) || (n == -1)) { break; } fstr.Write(inBuf, 0, n); bytesReadTotal += n; } str.Close(); fstr.Close(); 

but its not working and now i'm stuck with this

  string dLink = "https://www.loginpage.com/export_excel.php?export_type=list"; HttpWebRequest fileRequest = (HttpWebRequest)HttpWebRequest.Create(dLink); fileRequest.CookieContainer = new CookieContainer(); fileRequest.CookieContainer.Add(cookies); fileRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; HttpWebResponse fileResponse = (HttpWebResponse)fileRequest.GetResponse(); for (int i = 0; i < fileResponse.Headers.Count; ++i) richTextBox1.Text += "\nHeader Name: " + fileResponse.Headers.Keys[i] + ", Value :" + fileResponse.Headers[i]; 

Of course, this is not a file download. Am I trying to get headlines to just understand what I get from the Internet? I have already downloaded several files using a script from files such as rghost or filehippo, but this file does not work.

+4
source share
2 answers

That should work!

  CookieContainer cookieJar = new CookieContainer(); CookieAwareWebClient http = new CookieAwareWebClient(cookieJar); string postData = "name=********&password=*********&submit=submit"; string response = http.UploadString("https://www.loginpage.com/", postData); // validate your login! http.DownloadFile("https://www.loginpage.com/export_excel.php?export_type=list", "my_excel.xls"); 

I used CookieAwareWebClient

 public class CookieAwareWebClient : WebClient { public CookieContainer CookieContainer { get; set; } public Uri Uri { get; set; } public CookieAwareWebClient() : this(new CookieContainer()) { } public CookieAwareWebClient(CookieContainer cookies) { this.CookieContainer = cookies; } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = this.CookieContainer; } HttpWebRequest httpRequest = (HttpWebRequest)request; httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return httpRequest; } protected override WebResponse GetWebResponse(WebRequest request) { WebResponse response = base.GetWebResponse(request); String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie]; if (setCookieHeader != null) { //do something if needed to parse out the cookie. if (setCookieHeader != null) { Cookie cookie = new Cookie(); //create cookie this.CookieContainer.Add(cookie); } } return response; } } 

Source and credit for: CookieAwareWebClient

+5
source

If the client account is already a valid user of the site, you just need to use the default credentials before downloading, for example.

 WebClient Client=new WebClient(); Client.UseDefaultCredentials=true; Client.DownloadFile(url, destination); 
0
source

All Articles