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 f开发者_StackOverflow社区ile 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);
}
精彩评论