Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Threading.Tasks;namespacewebsitetest.common{ Public classHttpdldfile {/// <summary> ///http way to download files/// </summary> /// <param name= "url" >HTTP Address</param> /// <param name= "LocalFile" >Local File</param> /// <returns></returns> Public BOOLDownload (stringUrlstringLocalFile) { BOOLFlag =false; LongStartPosition =0;//last downloaded file start locationFileStream Writestream;//write to local file stream object Longremotefilelength = gethttplength (URL);//get remote file lengthSystem.Console.WriteLine ("remotefilelength="+remotefilelength); if(Remotefilelength = =745) { //System.Console.WriteLine ("Remote file does not exist."); return false; } //determine if the folder you want to download exists if(File.exists (LocalFile)) {//Writestream = File.openwrite (LocalFile); //exists then open the file you want to download//startposition = writestream.length; //get the length that has been downloaded//if (startposition >= remotefilelength)//{ // //System.Console.WriteLine ("Local file Length" + StartPosition + "is already greater than or equal to remote file length" + remotefilelength); //Writestream.close (); //return false; //} //Else//{ //Writestream.seek (startposition, seekorigin.current);//local File write location locator//} return false; } Else{Writestream=NewFileStream (LocalFile, FileMode.Create);//file is not saved create a fileStartPosition =0; } Try{HttpWebRequest Myrequest= (HttpWebRequest) httpwebrequest.create (URL);//Open Network Connection if(StartPosition >0) {Myrequest.addrange (int) startposition);//set range value, same as above Writestream.seek, to define remote file read location} Stream Readstream= Myrequest.getresponse (). GetResponseStream ();//request to the server, get the response data stream of the server byte[] Btarray =New byte[ +];//defines a byte of data used to read content to Readstream and write content to Writestream intContentsize = Readstream.read (Btarray,0, btarray.length);//read the first time to a remote file LongCurrpostion =startposition; while(Contentsize >0)//Continue reading if read length is greater than zero{currpostion+=contentsize; intPercent = (int) (Currpostion * -/remotefilelength); //System.Console.WriteLine ("percent=" + percent + "%");Writestream.write (Btarray,0, contentsize);//write to local fileContentsize = Readstream.read (Btarray,0, btarray.length);//continue to read to remote files } //Close the streamWritestream.close (); Readstream.close (); Flag=true;//return true Download succeeded } Catch(Exception) {writestream.close (); Flag=false;//return false Download failed } returnFlag; } //get the length of the remote file from the file header Private Static LongGethttplength (stringURL) { LongLength =0; Try{HttpWebRequest req= (HttpWebRequest) httpwebrequest.create (URL);//Open Network ConnectionHttpWebResponse RSP =(HttpWebResponse) req. GetResponse (); if(RSP.) StatusCode = =httpstatuscode.ok) {length= RSP. ContentLength;//get the length of the remote file from the file header} RSP. Close (); returnlength; } Catch(Exception e) {returnlength; } } }}
HTTP way to download files