使用httprequest下载文件
是否可以使用 httprequest 从网站下载文件?我只习惯用它来获取页面的源代码。如果无法使用 httprequest 来完成此操作,是否可以使用 C# 下载文件而无需使用网络浏览器?
编辑:答案必须允许我选择硬盘上将文件下载到的位置
Is it possible to download files from a website using httprequest? I am only used to using it to get source code of a page. If there is no way to do it using httprequest, is there a way to download files using C# without having to use the webbrowser?
Edit: The answer must allow me to chose the location on the hard drive where the file will be downloaded to
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您绝对可以通过获取
WebResponse
并使用其响应流来使用HttpRequest
。或者,使用WebClient
及其DownloadFile< /code>
和
DownloadData
让生活更轻松的方法。最终,获取二进制文件作为响应的请求与获取一些 HTML 作为响应的请求之间没有太大区别。在某些方面,二进制响应更容易处理,因为您无需担心字符编码。
You can absolutely use
HttpRequest
by getting theWebResponse
and using its response stream. Alternatively, useWebClient
, with itsDownloadFile
andDownloadData
methods to make life easier.Ultimately there's not much difference between a request which gets a binary file as a response and a request which gets some HTML as a response. In some ways a binary response is easier to deal with, as you don't need to worry about character encodings.
使用 WebClient 类 包含了通过 http 下载数据的所有需求。
获取页面的源代码:
use a WebClient Class that wraps all of your needs to download data over http.
to get the source code of a page:
这应该有效。
This should work.