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 !!! ");
}
}
}
}