Downloading a file on the Internet using C # is primarily dependent on httpwebrequest/httpwebresonse and WebClient. There are two different ways to deal with synchronization and asynchrony, so we actually have four combinations.
1. Using Httpwebrequest/httpwebresonse and WebClient
HttpWebRequest request = (HttpWebRequest) webrequest.create (URL);
WebResponse response = Request. GetResponse ();
Stream stream = Response. GetResponseStream ();
if (!response. Contenttype.tolower (). StartsWith ("text/"))
{
Value = Savebinaryfile (response, FileName);
byte[] buffer = new byte[1024];
Stream OutStream = System.IO.File.Create (FileName);
Stream instream = Response. GetResponseStream ();
int l;
Do
{
L = instream.read (buffer, 0, buffer. Length);
if (L > 0)
Outstream.write (buffer, 0, L);
}
while (L > 0);
Outstream.close ();
Instream.close ();
}
2. Using WebClient
String url = "Http://www.mozilla.org/images/feature-back-cnet.png";
WebClient mywebclient = new WebClient ();
Mywebclient.downloadfile (URL, "C:\\temp\\feature-back-cnet.png");
3. Asynchronous invocation
Asynchronous calls can be found in: http://wenku.baidu.com/view/f47cc781e53a580216fcfe64.html
Network Programming (i): 2 ways to download network files in C #