通过 HTTPS 下载文件 C# - Cookie 和标头问题?
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我前几天这样做了,总之,您需要创建 HttpWebRequest 和 HttpWepResponse 来提交/接收数据。 由于您需要跨多个请求维护 cookie,因此您需要创建一个 cookie 容器来保存您的 cookie。 如果需要,您也可以在请求/响应上设置标头属性...
基本概念:
解决此问题的关键是捕获真实请求的流量。 我使用 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:
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
这个人编写了一个使用 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?
我在 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