简体   繁体   中英

Downloading files using httprequest

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

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.

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

This should work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM