ivy_live521£¨Ô×÷£©
¶ÏµãÐø´«µÄÔÀí
ÔÚÁ˽âHTTP¶ÏµãÐø´«µÄÔÀí֮ǰ£¬ÏÈÀ´ËµËµHTTPÐÒ飬HTTPÐÒéÊÇÒ»ÖÖ»ùÓÚtcpµÄ¼òµ¥ÐÒ飬·ÖΪÇëÇóºÍ»Ø¸´Á½ÖÖ¡£ÇëÇóÐÒéÊÇÓÉ¿Í»§»ú(ä¯ÀÀÆ÷)Ïò·þÎñÆ÷£¨WEB SERVER£©Ìá½»ÇëÇóʱ·¢Ëͱ¨ÎĵÄÐÒé¡£»Ø¸´ÐÒéÊÇÓÉ·þÎñÆ÷(web server)£¬Ïò¿Í»§»ú(ä¯ÀÀÆ÷)»Ø¸´±¨ÎÄʱµÄÐÒé¡£ÇëÇóºÍ»Ø¸´ÐÒ鶼ÓÉÍ·ºÍÌå×é³É¡£Í·ºÍÌåÖ®¼äÒÔÒ»ÐпÕÐÐΪ·Ö¸ô¡£
ÒÔÏÂÊÇÒ»¸öÇëÇó±¨ÎÄÓëÏàÓ¦µÄ»Ø¸´±¨ÎĵÄÀý×Ó£º
GET /image/index_r4_c1.jpg HTTP/1.1
Accept: */*
Referer: http://192.168.3.120:8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)
Host: 192.168.3.120:8080
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 24 Jun 2003 05:39:40 GMT
Content-Type: image/jpeg
Accept-Ranges: bytes
Last-Modified: Thu, 23 May 2002 03:05:40 GMT
ETag: "bec48eb862c21:934"
Content-Length: 2827
ÿ? JFIF H H ÿ C [1]
¡.
ÏÂÃæÎÒÃǾÍÀ´ËµËµ¡°¶ÏµãÐø´«¡±¡£
¹ËÃû˼Ò壬¶ÏµãÐø´«¾ÍÊÇÔÚÉÏÒ»´ÎÏÂÔØʱ¶Ï¿ªµÄλÖÿªÊ¼¼ÌÐøÏÂÔØ¡£ÔÚHTTPÐÒéÖУ¬¿ÉÒÔÔÚÇëÇó±¨ÎÄÍ·ÖмÓÈëRange¶Î£¬À´±íʾ¿Í»§»úÏ£Íû´ÓºÎ´¦¼ÌÐøÏÂÔØ¡£
±ÈÈç˵´ÓµÚ1024×Ö½Ú¿ªÊ¼ÏÂÔØ£¬ÇëÇó±¨ÎÄÈçÏ£º
GET /image/index_r4_c1.jpg HTTP/1.1
Accept: */*
Referer: http://192.168.3.120:8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)
Host: 192.168.3.120:8080
Range:bytes=1024-
Connection: Keep-Alive
.NETÖеÄÏà¹ØÀà
Ã÷°×ÁËÉÏÃæµÄÔÀí£¬ÄÇô£¬ÎÒÃÇÀ´¿´¿´.NET FRAMEWORKÖÐΪÎÒÃÇÌṩÁËÄÄЩÀà¿ÉÒÔÀ´×öÕâЩÊ¡£
Íê³ÉHTTPÇëÇó
System.Net.HttpWebRequest
HttpWebRequest Àà¶Ô WebRequest Öж¨ÒåµÄÊôÐԺͷ½·¨Ìṩ֧³Ö£¬Ò²¶ÔʹÓû§Äܹ»Ö±½ÓÓëʹÓà HTTP µÄ·þÎñÆ÷½»»¥µÄ¸½¼ÓÊôÐԺͷ½·¨Ìṩ֧³Ö¡£
HttpWebRequest ½«·¢Ë͵½ Internet ×ÊÔ´µÄ¹«¹² HTTP ±êÍ·Öµ¹«¿ªÎªÊôÐÔ£¬ÓÉ·½·¨»òϵͳÉèÖá£Ï±í°üº¬ÍêÕûÁÐ±í¡£¿ÉÒÔ½« Headers ÊôÐÔÖеÄÆäËû±êÍ·ÉèÖÃΪÃû³Æ/Öµ¶Ô¡£µ«ÊÇ×¢Ò⣬ijЩ¹«¹²±êÍ·±»ÊÓΪÊÜÏÞÖƵģ¬ËüÃÇ»òÕßÖ±½ÓÓÉ API¹«¿ª£¬»òÕßÊܵ½ÏµÍ³±£»¤£¬²»Äܱ»¸ü¸Ä¡£RangeÒ²ÊôÓÚ±»±£»¤Ö®ÁУ¬²»¹ý£¬.NETΪ¿ª·¢ÕßÌṩÁ˸ü·½±ãµÄ²Ù×÷£¬¾ÍÊÇ AddRange·½·¨£¬ÏòÇëÇóÌí¼Ó´ÓÇëÇóÊý¾ÝµÄ¿ªÊ¼´¦»ò½áÊø´¦µÄÌض¨·¶Î§µÄ×Ö½Ú·¶Î§±êÍ·
Íê³ÉÎļþ·ÃÎÊ
System.IO.FileStream
FileStream ¶ÔÏóÖ§³ÖʹÓÃSeek·½·¨¶ÔÎļþ½øÐÐËæ»ú·ÃÎÊ, Seek ÔÊÐí½«¶ÁÈ¡/дÈëλÖÃÒƶ¯µ½ÎļþÖеÄÈÎÒâλÖá£ÕâÊÇͨ¹ý×Ö½ÚÆ«ÒƲο¼µã²ÎÊýÍê³ÉµÄ¡£×Ö½ÚÆ«ÒÆÁ¿ÊÇÏà¶ÔÓÚ²éÕҲο¼µã¶øÑԵģ¬¸Ã²Î¿¼µã¿ÉÒÔÊÇ»ù´¡ÎļþµÄ¿ªÊ¼¡¢µ±Ç°Î»Öûò½á⣬·Ö±ðÓÉSeekOriginÀàµÄÈý¸öÊôÐÔ±íʾ¡£
´úÂëʵÏÖ
Á˽âÁË.NETÌṩµÄÏà¹ØµÄÀ࣬ÄÇô£¬ÎÒÃǾͿÉÒÔ·½±ãµÄʵÏÖÁË¡£
´úÂëÈçÏ£º
static void Main(string[] args)
{
string StrFileName="c:\\aa.zip"; //¸ù¾Ýʵ¼ÊÇé¿öÉèÖÃ
string StrUrl="http://www.xxxx.cn/xxxxx.zip"; //¸ù¾Ýʵ¼ÊÇé¿öÉèÖÃ
//´ò¿ªÉÏ´ÎÏÂÔصÄÎļþ»òн¨Îļþ
long lStartPos =0;
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs= System.IO.File.OpenWrite(StrFileName);
lStartPos=fs.Length;
fs.Seek(lStartPos,System.IO.SeekOrigin.Current); //Òƶ¯ÎļþÁ÷Öеĵ±Ç°Ö¸Õë
}
else
{
fs = new System.IO.FileStream(StrFileName,System.IO.FileMode.Create);
lStartPos =0;
}
//´ò¿ªÍøÂçÁ¬½Ó
try
{
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
if ( lStartPos>0)
request.AddRange((int)lStartPos); //ÉèÖÃRangeÖµ
//Ïò·þÎñÆ÷ÇëÇ󣬻ñµÃ·þÎñÆ÷»ØÓ¦Êý¾ÝÁ÷
System.IO.Stream ns= request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize=0;
nReadSize=ns.Read(nbytes,0,512);
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,512);
}
fs.Close();
ns.Close();
Console.WriteLine("ÏÂÔØÍê³É");
}
catch(Exception ex)
{
fs.Close();
Console.WriteLine("ÏÂÔعý³ÌÖгöÏÖ´íÎó:"+ex.ToString());
}
}
ÒÔÉÏÊDZ¾ÈËÔÚ¿ª·¢ÖеÄÒ»µãССÌåÑ飬ϣÍûÄÜÓë´ó¼Ò·ÖÏí£¡ £º£©
×¢£º±¾ÎÄδ¾Ðí¿É£¬²»µÃתÔØ