Common ways to download HTTP requests in C # demo

Source: Internet
Author: User

by WebClient Way

WebClient client = new WebClient ();
String url= "Http://down6.3987.com:801/2010/office_3987.com.zip";
Stream STRM = client. OpenRead ("Http://down6.3987.com:801/2010/office_3987.com.zip");
String Filename=url. Substring (URL. LastIndexOf ('/') +1);
int count = 0;
byte[] buffer = new byte[4096];
FileStream fs = new FileStream (application.startuppath+ "//" +filename, FileMode.Create);
while ((count = Strm. Read (buffer, 0, buffer. Length)) > 0) {

Fs. Write (buffer, 0, count);

}

Fs. Close ();
Strm. Close ();
Strm. Dispose ();
Fs. Dispose ();

Through the original ecological HttpWebRequest Way

String url = "Http://down6.3987.com:801/2010/office_3987.com.zip";
string filename = URL. Substring (URL. LastIndexOf ('/') + 1);
Create an HTTP request in WebClient mode
HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (URL); Create a link

Get Service its data
HttpWebResponse res = (HttpWebResponse) req. GetResponse ();

Stream MyStream = Res. GetResponseStream ();

Start file operation
FileStream fs = new FileStream (appdomain.currentdomain.basedirectory+ "//" +filename, FileMode.Create, FileAccess.Write, Fileshare.none);

Byte[] Buffer=new byte[1024];
int count=0;
while ((Count=mystream. Read (buffer,0,1024)) >0)
{
Fs. Write (buffer, 0, count);
}

Fs. Close ();
MyStream. Close ();
Fs. Dispose ();

Common ways to download HTTP requests in C # demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.