Download file over HTTPS C # - Cookie and Header Prob?

I am trying to upload a file over HTTPS and I just keep working in a brick wall with the Cookies and Headers set up correctly.

Does anyone know / knows any code that I can review to do this correctly? download file via https and set cookies / headers?

Thanks!

+1


a source to share


3 answers


I did it the other day, in the end you need to create HttpWebRequest and HttpWepResponse to send / receive data. Since you need to support cookies across multiple requests, you need to create a cookie container to store your cookies. You can also set header properties on request and response.

Basic concept:

Using System.Net;

// Create Cookie Container (Place to store cookies during multiple requests)

CookieContainer cookies = new CookieContainer();

// Request Page
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.amazon.com");
req.CookieContainer = cookies;

// Response Output (Could be page, PDF, csv, etc...)

HttpWebResponse resp= (HttpWebResponse)req.GetResponse();

// Add Response Cookies to Cookie Container
// I only had to do this for the first "login" request

cookies.Add(resp.Cookies);

      

The key to figuring this out is capturing traffic for a real request. I did it using Fiddler and over the course of several captures (almost 10), I figured out what I needed to do to reproduce a login on a site where I needed to run multiple reports based on different selection criteria (date range, parts, etc. etc.) and upload the results to CSV files. It works perfect, but Fiddler was the key to figuring it out.



http://www.fiddler2.com/fiddler2/

Good luck.

Zach

+2


a source


This employee wrote an application to download files using HTTP:

http://www.codeproject.com/KB/IP/DownloadDemo.aspx



Not really sure what you mean when setting cookies and headers. Is this required on the site you are downloading? If so, what cookies and headers should be set?

0


a source


I got lucky with the WebClient class. This is a wrapper for HttpWebRequest which can save a few lines of code: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

0


a source







All Articles