// 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 ();}}