Downloading files using httprequest

北城以北 提交于 2019-12-11 13:04:58

问题


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


回答1:


You can absolutely use HttpRequest by getting the WebResponse and using its response stream. Alternatively, use WebClient, with its DownloadFile and DownloadData 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.




回答2:


use a WebClient Class that wraps all of your needs to download data over http.

to get the source code of a page:

 WebClient client = new WebClient ();
 string src = client.DownloadString(uri);



回答3:


This should work.

using (WebClient wc = new WebClient())
{
    wc.DownloadFile(downloadURL, fileName);
}


来源:https://stackoverflow.com/questions/4856895/downloading-files-using-httprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!