通过 HTTPS 下载文件 C# - Cookie 和标头问题?

发布于 2024-07-22 23:23:45 字数 143 浏览 3 评论 0原文

我正在尝试通过 HTTPS 下载文件,但在正确设置 Cookie 和标头方面却总是遇到困难。

有谁知道/知道我可以检查以正确执行此操作的任何代码吗? 即通过 https 下载文件并设置 cookies/headers ?

谢谢!

I am trying to download a file over HTTPS and I just keep running into a brick wall with correctly setting Cookies and Headers.

Does anyone have/know of any code that I can review for doing this correctly ? i.e. download a file over https and set cookies/headers ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热风软妹 2024-07-29 23:23:45

我前几天这样做了,总之,您需要创建 HttpWebRequest 和 HttpWepResponse 来提交/接收数据。 由于您需要跨多个请求维护 cookie,因此您需要创建一个 cookie 容器来保存您的 cookie。 如果需要,您也可以在请求/响应上设置标头属性...

基本概念:

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);

解决此问题的关键是捕获真实请求的流量。 我使用 Fiddler 完成了此操作,在几次捕获(几乎 10 次)的过程中,我弄清楚了我需要做什么来重现登录到一个站点,在该站点中我需要根据不同的选择标准(日期范围、部分)运行一些报告等)并将结果下载到 CSV 文件中。 它运行完美,但 Fiddler 是解决这个问题的关键。

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

祝你好运。

扎克

I did this the other day, in summary you need to create a HttpWebRequest and HttpWepResponse to submit/receive data. Since you need to maintain cookies across multiple requests, you need to create a cookie container to hold your cookies. You can set header properties on request/response if needed as well....

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 the traffic for real request. I did this using Fiddler and over the course of a few captures (almost 10), I figured out what I need to do to reproduce the login to a site where I needed to run some reports based on different selection critera (date range, parts, etc..) and download the results into CSV files. It's working perfect, but Fiddler was the key to figuring it out.

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

Good Luck.

Zach

你对谁都笑 2024-07-29 23:23:45

这个人编写了一个使用 HTTP 下载文件的应用程序:

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

不太清楚设置 cookie 和标头是什么意思。 您下载的网站是否要求这样做? 如果是,需要设置哪些 cookie 和标头?

This fellow wrote an application to download files using HTTP:

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

Not quite sure what you mean by setting cookies and headers. Is that required by the site you are downloading from? If it is, what cookies and headers need to be set?

笑叹一世浮沉 2024-07-29 23:23:45

我在 WebClient 课程上很幸运。 它是 HttpWebRequest 的包装器,可以节省几行代码: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

I've had good luck with the WebClient class. It's a wrapper for HttpWebRequest that can save a few lines of code: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文