Using XMLHTTP, the implementation of the code is simple and concrete implementation is as follows:
First the bin file is introduced, Com->microsoft XML v3.0
The specific code is as follows:
protected voidButton1_Click (Objectsender, EventArgs e) { stringFileNames ="201406251824392435.pdf", ContentType =""; stringHouzhui = filenames.substring (Filenames.lastindexof (".")); ContentType=Checktype (FileNames); stringURL ="http://xxx.xxx.xxx.xxx:8089/upfile/wenku/"+filenames;//domain name or IP addressstringStringfilename = url.substring (Url.lastindexof ("/") +1); stringStringfilepath =Request.physicalapplicationpath; if(! Stringfilepath.endswith ("/")) Stringfilepath + ="/"; Try{MSXML2. XMLHTTP _xmlhttp=NewMSXML2. Xmlhttpclass (); _xmlhttp.open ("GET"Urlfalse,NULL,NULL); _xmlhttp.send (""); if(_xmlhttp.readystate = =4)//Data Reception Complete { if(_xmlhttp.status = = $)//whether to return the correct data{HttpContext.Current.Response.AddHeader ("content-disposition","attachment; Filename="+ Httputility.urlencode ("Test"+Houzhui, System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType=ContentType; HttpContext.Current.Response.BinaryWrite ((byte[]) _xmlhttp.responsebody);//output binary to browserHttpContext.Current.Response.Flush (); HttpContext.Current.Response.End (); } Else{Response.Write ("Error:"+ _xmlhttp.statustext +"the file you want to download is not found!"); } } Else{Response.Write ("Error:"+ _xmlhttp.statustext +"the HTTP address of the request is not correct! "); Response.End (); } } Catch(Exception ex) {Response.Write ("Error: Unable to find the requested resource! "); Response.End (); } }
Returns the MIME type:
Private Static stringChecktype (stringfilename) { stringContentType; stringHouzhui = filename. Substring (filename. LastIndexOf (".") +1). Trim (). ToLower (); Switch(Houzhui) { Case ". asf": ContentType="video/x-ms-asf"; Break; Case ". avi": ContentType="Video/avi"; Break; Case ". doc": ContentType="Application/msword"; Break; Case ". zip": ContentType="Application/zip"; Break; Case ". xls": ContentType="Application/vnd.ms-excel"; Break; Case ". gif": ContentType="Image/gif"; Break; Case ". jpg": ContentType="Image/jpeg"; Break; Case "JPEG": ContentType="Image/jpeg"; Break; Case ". wav": ContentType="Audio/wav"; Break; Case ". mp3": ContentType="audio/mpeg3"; Break; Case ". mpg": ContentType="Video/mpeg"; Break; Case ". MEPG": ContentType="Video/mpeg"; Break; Case ". rtf": ContentType="application/rtf"; Break; Case ". html": ContentType="text/html"; Break; Case ". htm": ContentType="text/html"; Break; Case ". txt": ContentType="Text/plain"; Break; Case ". pdf": ContentType="application/pdf"; Break; default: ContentType="Application/octet-stream"; Break; } returnContentType; }
Download the file and save it to this machine:
<summary>////Get remote file to local///</summary>//<param name= "url" > Remote file Address </param>/// Lt;param name= "path" > Local path </param> private static void Downurltofile (string url, string path) {Strin G Cookie = String.Empty; String refer = URL. Substring (0, URL. LastIndexOf ("/") + 1); System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create (URL) as System.Net.HttpWebRequest; Req. AllowAutoRedirect = false; Req. Referer = refer; Req. useragent = "mozilla/5.0 (Windows; U Windows NT 6.1; ZH-CN; rv:1.9.2.13) gecko/20101203 firefox/3.6.13 "; System.Net.HttpWebResponse res = req. GetResponse () as System.Net.HttpWebResponse; System.Net.WebHeaderCollection headers = Res. Headers; String Newurl = ""; if (res. StatusCode = = System.Net.HttpStatusCode.Found) | | (Res. StatusCode = = System.Net.HttpStatusCode.Redirect) | | (Res. StatusCode = = System.Net.HttpStatusCode.Moved) | | (Res. StatusCode = = System.Net.HttpStatusCode.MovedPermanently)) {Newurl = headers["Location"]; Newurl = Newurl.trim (); } if (headers["Set-cookie"]! = null) {Cookie = headers["Set-cookie"]; } res. Close (); req = null; String fileName = newurl.substring (Newurl.lastindexof ("/") + 1); req = System.Net.HttpWebRequest.Create (URL) as System.Net.HttpWebRequest; Req. AllowAutoRedirect = true; Req. Referer = URL; Req. useragent = "mozilla/5.0 (Windows; U Windows NT 6.1; ZH-CN; rv:1.9.2.13) gecko/20101203 firefox/3.6.13 "; res = req. GetResponse () as System.Net.HttpWebResponse; System.IO.Stream Stream = Res. GetResponseStream (); byte[] buffer = new BYTE[32 * 1024]; int bytesprocessed = 0; System.IO.FileStream fs = System.IO.File.Create (HttpContext.Current.Server.MapPath (path)); int bytesread; do {bytesread = StReam. Read (buffer, 0, buffer. Length); Fs. Write (buffer, 0, bytesread); bytesprocessed + = Bytesread; } while (Bytesread > 0); Fs. Flush (); Fs. Close (); Res. Close (); }