C # HTTP protocol breakpoint continuation

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;namespacehttpdemo{classprogram{//the path to downloadStatic stringURL ="Http://www.aseoe.com/demo/api/jquery_aseoe_001.zip";//get full file nameStatic stringFileName =path.getfilename (URL);//Save PathStatic stringLocalPath ="d:\\httpceshi\\"+FileName;//The appended. temp suffix name is not finished downloadingStatic stringTempPath = LocalPath +". Temp";//offset (breakpoint)Static intPos;Static voidMain (string[] args) {DownLoad (); Console.ReadLine ();} Public Static voidDownLoad () {//End If there is a file to updateif(File.exists (localPath))return; Stream Localfs=NULL;//If no files have been uploaded (requires a breakpoint to continue file)if(File.exists (TempPath)) {Localfs=NewFileStream (TempPath, FileMode.Open);}Else{Localfs=NewFileStream (TempPath, FileMode.Create);}//Get Breakpointpos = (int) Localfs. Length; HttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); request. Headers.add ("Accept-charset","Gbk,utf-8"); request. UserAgent="mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. NET CLR 1.0.3705;)"; request. Method="GET"; request. Timeout=30000;//Total file length unit (B)LongTotal =request. GetResponse (). ContentLength; Console.WriteLine ("The total length is:"+Total );if(Pos >0) Request. AddRange (POS);//Offset to breakpointHttpWebResponse Responseresult=(HttpWebResponse) request. GetResponse ();using(Stream stream =Responseresult.getresponsestream ()) {byte[] buffer =New byte[Total-POS];intReadSize = stream. Read (Buffer,0, buffer. Length); while(ReadSize >0) {Localfs. Write (Buffer,0, readsize); ReadSize= Stream. Read (Buffer,0, buffer. Length); Console.WriteLine ("have downloaded:"+Localfs. Length);} Localfs. Close (); File.move (TempPath, localPath);//change temporary file to normal file suffixConsole.WriteLine ("Download Complete!!!");}}}}

C # HTTP protocol breakpoint continuation

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.