起因:公司有個項目用到了斷點續傳,之前開發了一套但是由於現場環境的問題導致不能用(原理是用aspnet_isapi.dll來響應PUT和POST請求),後來考慮換個模式,當中要感謝協助過我的人(pkwsh、egmkang等)
1、斷點續傳的原理(來自網路)
在瞭解HTTP斷點續傳的原理之前,先來說說HTTP協議,HTTP協議是一種基於tcp的簡單協議,分為請求和回複兩種。
請求協議是由客戶機(瀏覽器)向伺服器(WEB SERVER)提交請求時發送報文的協議。回複協議是由伺服器(web ser
ver),向客戶機(瀏覽器)回複報文時的協議。請求和回複協議都由頭和體組成。頭和體之間以一行空行為分隔。
以下是一個請求報文與相應的回複報文的例子:
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
….
下面我們就來說說“斷點續傳”。
顧名思義,斷點續傳就是在上一次下載時斷開的位置開始繼續下載。在HTTP協議中,可以在請求報文頭中加入Rang
e段,來表示客戶機希望從何處繼續下載。
比如說從第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 屬性中的其他標題設定為成對的名稱和數值。但是注意,某些公用標題被視為受限制的,它們或者直接
由 API公開,或者受到系統保護,不能被更改。Range也屬於被保護之列,不過,.NET為開發人員提供了更方便的操作
,就是 AddRange方法,向請求添加從請求資料的開始處或結束處的特定範圍的位元組範圍標題
完成檔案訪問
System.IO.FileStream
FileStream 對象支援使用Seek方法對檔案進行隨機訪問, Seek 允許將讀取/寫入位置移動到檔案中的任意位置。這
是通過位元組位移參考點參數完成的。位元組位移量是相對於尋找參考點而言的,該參考點可以是基礎檔案的開始、當
前位置或結尾,分別由SeekOrigin類的三個屬性工作表示。
2、網上的可以跑的樣本
代碼實現
瞭解了.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());
}
3、服務端的配置
配置一個網站,並給Asp.Net和NetWorkService用讀取並運行許可權
4、完善(加上簡單邏輯、多線程)
- #region 作者:wt0731, 2010-01-02
- /*
- * 版本 :1.0
- * 維護者:
- * 最近維護日期:
- *
- */
- #endregion
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace DownloadFile
- {
- public partial class FormDownLoad : Form
- {
- private Thread threadDownload = null;
- private Thread threadUpload = null;
- /// <summary>
- /// 下載路徑
- /// </summary>
- private string DownloadPath = "http://192.168.100.46/HttpHandle/2.rar";//根據實際需要修改
- /// <summary>
- /// 本機存放區路徑
- /// </summary>
- private string localSourceFile = string.Empty;
- /// <summary>
- /// 暫停
- /// </summary>
- private bool dlThreadContinue = false;
- public FormDownLoad()
- {
- InitializeComponent();
- this.btnStop.Enabled = false;
- }
- private void btnDownLoad_Click(object sender, EventArgs e)
- {
- Cursor.Current = Cursors.WaitCursor;
- this.btnDownLoad.Enabled = false;
- this.dlThreadContinue = true;
- this.btnStop.Enabled = true;
- localSourceFile = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + this.txtName.Text.ToString(); //根據實際情況設定
- DownloadPath = this.txtUrl.Text.ToString(); //根據實際情況設定
- threadDownload = new Thread(new ThreadStart(DownloadThread));
- threadDownload.Start();
- }
- /// <summary>
- /// 下載線程的入口函數
- /// </summary>
- void DownloadThread()
- {
- DownloadFile(this.DownloadPath,this.localSourceFile);
- }
- /// <summary>
- /// 下載檔案,無壓縮傳輸
- /// </summary>
- /// <param name="serverUrl">服務端處理Http請求的地址</param>param>
- /// <param name="fileName">本機存放區的檔案名稱</param>
- private void DownloadFile(string serverUrl, string fileName)
- {
- //開啟上次下載的檔案或建立檔案
- long lStartPos = 0;
- System.IO.FileStream fs;
- if (System.IO.File.Exists(fileName))
- {
- fs = System.IO.File.OpenWrite(fileName);
- lStartPos = fs.Length;
- fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移動檔案流中的當前指標
- }
- else
- {
- fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
- lStartPos = 0;
- }
- //開啟網路連接
- try
- {
- System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(serverUrl);
- request.KeepAlive = true;
- if (lStartPos > 0)
- request.AddRange((int)lStartPos); //設定Range值
- System.Text.Encoding utf8 = System.Text.Encoding.Default;
- //向伺服器請求,獲得伺服器回應資料流
- System.IO.Stream ns = request.GetResponse().GetResponseStream();
- byte[] nbytes = new byte[1024];
- int nReadSize = 0;
- nReadSize = ns.Read(nbytes, 0, 1024);
- while (nReadSize > 0 && dlThreadContinue)
- {
- fs.Write(nbytes, 0, nReadSize);
- nReadSize = ns.Read(nbytes, 0, 1024);
- }
- //fs.Flush();
- fs.Close();
- ns.Close();
- Cursor.Current = Cursors.Default;
- MessageBox.Show("下載完成", "系統提示");
- }
- catch (Exception ex)
- {
- Cursor.Current = Cursors.Default;
- fs.Close();
- MessageBox.Show("下載過程中出現錯誤:" + ex.ToString(), "系統提示");
- }
- }
- private void menuItem1_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- if (this.btnStop.Text == "暫停")
- {
- this.dlThreadContinue = false;
- Cursor.Current = Cursors.Default;
- this.btnDownLoad.Enabled = true;
- this.btnStop.Enabled = false;
- }
- }
- }
- }
複製代碼
擴充資料:
HttpWebRequest&HttpWebResponse Headers
介紹
這裡簡要介紹如何使用HttpWebRequest&HttpWebResponse兩個對象與HTTP伺服器進行直接互動的過程.HttpWebRequest類對WebRequest中定義的屬性和方法提供支援,在使用HttpWebRequest對象向HTTP伺服器發起請求時請不要使用HttpWebRequest對象的建構函式,而應該使用WebRequest.Create()方法來初始化新的HttpWebRequest對象.如果統一資源識別項方案是"http://"或"https://"時,Create()則返回HttpWebResponse對象.
代碼
首先,我們需要建立一個新的HttpWebRequest對象,代碼如下:
HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(new Uri("urlstring"));
注意:上文中已經提到過不要使用HttpWebRequest的建構函式來建立對象;使用WebRequest.Create()方法初始化HttpWebRequest對象時應該對其進行類型轉換.
接下來可以對新初始化的對象進行簡單操作,比如可以設定它的標題屬性,
下表列出了由屬性或方法設定或由系統設定的標題:
________________________________________
標題 設定方法
Accept 由Accept屬性設定
Connection 由Connection屬性和KeepAlive屬性設定
Content-Length 由ContentLength屬性設定
Content-Type 由ContentType屬性設定
Expect 由Expect屬性設定
Date 由系統設定為當前日期
Host 由系統設定為當前主機資訊
If-Modified-Since 由IfModifiedSince屬性設定
Range 由Range屬性設定
Transfer-Encoding 由TransferEncoding屬性設定
Referer 由Referer屬性設定
User-Agent 由UserAgent屬性設定
________________________________________
注意:HttpWebRequest自動註冊.使用以"http://"或"https://"開頭的URL之前,不需要調用RegisterPrefix方法來註冊
源碼下載: 測試版本源碼DownloadFile.rar (354.25 KB)