HTTP way to download files

Source: Internet
Author: User

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

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.