HTTP breakpoint resume

Source: Internet
Author: User

HTTP breakpoint resume

Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Linq;
Using System. Net;
Using System. Text;

Namespace HttpDemo
{
Class Program
{
// Download path
Static string url = "http://www.aseoe.com/demo/api/jquery_aseoe_001.zip ";
// Get the full name of the file
Static string fileName = Path. GetFileName (url );
// Save path
Static string localPath = "D :\\ HttpCeShi \" + fileName;
// Add the. temp extension after the download.
Static string tempPath = localPath + ". temp ";
// Offset (breakpoint)
Static int pos;

Static void Main (string [] args)
{
DownLoad ();
Console. ReadLine ();
}
Public static void DownLoad ()
{
// If the file to be updated exists, it will end.
If (File. Exists (localPath ))
Return;

Stream localfs = null;
// If no file has been uploaded (resumable upload is required)
If (File. Exists (tempPath ))
{
Localfs = new FileStream (tempPath, FileMode. Open );
}

Else
{
Localfs = new FileStream (tempPath, FileMode. Create );
}
// Obtain the breakpoint
Pos = (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)
Long total = request. GetResponse (). ContentLength;
Console. WriteLine ("total length:" + total );
If (pos> 0)
Request. AddRange (pos); // offset to the breakpoint

HttpWebResponse responseResult = (HttpWebResponse) request. GetResponse ();
Using (Stream stream = responseResult. GetResponseStream ())
{
Byte [] buffer = new byte [total-pos];

Int readSize = stream. Read (buffer, 0, buffer. Length );
While (readSize> 0)
{
Localfs. Write (buffer, 0, readSize );
ReadSize = stream. Read (buffer, 0, buffer. Length );
Console. WriteLine ("downloaded:" + localfs. Length );
}
Localfs. Close ();
File. Move (tempPath, localPath); // change the temporary File to a normal File suffix.
Console. WriteLine ("Download complete !!! ");
}

}


}
}

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.