Csharp: WebClient and WebRequest use http download file

Source: Internet
Author: User

// Csharp: WebClient and WebRequest use http download file // 20140318 collect plain string filePath = "20140302.133"; string fileName =" http://www.dusystem.com/3.pdf "; // 1 the file filePath // string headerValue = (Request. UserAgent. ToLower (). Contains (" msie ") cannot be found "))? String. format ("attachment; filename = \" {0} \ "", Uri. escapeDataString (fileName): string. format ("attachment; filename = \" {0} \ "", fileName); // for Firefox, Chrome, Safari, Opera // Response. clear (); // Response. contentType = "text/plain"; // Response. addHeader ("Content-Disposition", headerValue); // Response. transmitFile (filePath); // Response. end (); // 2: Valid WebException ex // System. net. webClient net = new System. net. webClient (); // string link = fileName; // Response. clearHeaders (); // Response. clear (); // Response. expires = 0; // Response. buffer = true; // Response. addHeader ("Accept-Language", "UTF-8"); // Response. addHeader ("Content-Disposition", "Attachment; FileName =" + System. web. httpUtility. urlEncode (link, System. text. encoding. UTF8); // Response. contentType = "APPLICATION/octet-stream"; // Response. bi NaryWrite (net. downloadData (link); // Response. end (); // 3: valid, but the file is empty // string dir = Environment. getFolderPath (Environment. specialFolder. optional topdirectory); // string myFileName = dir + @ "\ 213.20."; // HttpWebRequest request = (HttpWebRequest) WebRequest. create (fileName); // request. maximumResponseHeadersLength = 40000; // request. timeout = 20000; // request. credentials = CredentialCache. defaultCredential S; // HttpWebResponse response = (HttpWebResponse) request. getResponse (); // Stream receiveStream = response. getResponseStream (); // StreamReader readStream = new StreamReader (receiveStream, Encoding. UTF8); // using (StreamWriter sw = new StreamWriter (myFileName, false, Encoding. UTF8) // {// sw. write (readStream. readToEnd (); // sw. flush (); // sw. close (); //} // 4. Valid // byte [] result; // byte [] buffer = new byte [4096]; // string link = fileName; // WebRequest wr = WebRequest. create (fileName); // WebResponse response = wr. getResponse (); // Stream responseStream = response. getResponseStream (); // MemoryStream memoryStream = new MemoryStream (); // Response. addHeader ("Accept-Language", "UTF-8"); // Response. addHeader ("Content-Disposition", "Attachment; FileName =" + System. Web. httpUtility. urlEncode (link, System. text. encoding. UTF8); // Response. contentType = "APPLICATION/octet-stream"; // int count = 0; // do // {// count = responseStream. read (buffer, 0, buffer. length); // memoryStream. write (buffer, 0, count); // Response. binaryWrite (buffer); // Response. outputStream. write (buffer, 0, count); //} // while (count! = 0); // result = memoryStream. ToArray (); // 5. Valid string dir = Environment. getFolderPath (Environment. specialFolder. optional topdirectory); string fullFileName = dir + @ "\ 213.20."; HttpWebRequest hRequest = (HttpWebRequest) WebRequest. create (fileName); WebResponse response = hRequest. getResponse (); using (response = (HttpWebResponse) hRequest. getResponse () {/* Download the file to the fullFileName location */Stream streamResponse = response. getRespon SeStream (); if (streamResponse! = Null) {byte [] inBuf = new byte [response. contentLength]; int bytesToRead = System. convert. toInt32 (inBuf. length); int bytesRead = 0; while (bytesToRead> 0) {int n = streamResponse. read (inBuf, bytesRead, bytesToRead); if (n = 0) {break;} bytesRead + = n; bytesToRead-= n;} FileStream fstr = new FileStream (fullFileName, fileMode. openOrCreate, FileAccess. write); fstr. write (inBuf, 0, bytesRead); streamResponse. close (); streamResponse. dispose (); fstr. close (); fstr. dispose ();}}

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.