【一步一步】[更新]WindowsMobile上實現簡單斷點續傳

來源:互聯網
上載者:User

起因:公司有個項目用到了斷點續傳,之前開發了一套但是由於現場環境的問題導致不能用(原理是用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、完善(加上簡單邏輯、多線程)

  1. #region 作者:wt0731, 2010-01-02
  2. /*
  3. * 版本 :1.0
  4. * 維護者:
  5. * 最近維護日期:
  6. *
  7. */
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using System.Threading;
  17. namespace DownloadFile
  18. {
  19.     public partial class FormDownLoad : Form
  20.     {
  21.         private Thread threadDownload = null;
  22.         private Thread threadUpload = null;
  23.         /// <summary>
  24.         /// 下載路徑
  25.         /// </summary>
  26.         private string DownloadPath = "http://192.168.100.46/HttpHandle/2.rar";//根據實際需要修改
  27.         /// <summary>
  28.         /// 本機存放區路徑
  29.         /// </summary>
  30.         private string localSourceFile = string.Empty;
  31.         /// <summary>
  32.         /// 暫停
  33.         /// </summary>
  34.         private bool dlThreadContinue = false;
  35.         public FormDownLoad()
  36.         {
  37.             InitializeComponent();
  38.             this.btnStop.Enabled = false;
  39.         }
  40.         private void btnDownLoad_Click(object sender, EventArgs e)
  41.         {
  42.             Cursor.Current = Cursors.WaitCursor;
  43.             this.btnDownLoad.Enabled = false;
  44.             this.dlThreadContinue = true;
  45.             this.btnStop.Enabled = true;
  46.             localSourceFile = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + this.txtName.Text.ToString(); //根據實際情況設定
  47.             DownloadPath = this.txtUrl.Text.ToString(); //根據實際情況設定
  48.             threadDownload = new Thread(new ThreadStart(DownloadThread));
  49.             threadDownload.Start();            
  50.         }
  51.         /// <summary>
  52.         /// 下載線程的入口函數
  53.         /// </summary>
  54.         void DownloadThread()
  55.         {         
  56.              DownloadFile(this.DownloadPath,this.localSourceFile);         
  57.         }
  58.          /// <summary>
  59.         /// 下載檔案,無壓縮傳輸
  60.         /// </summary>
  61.         /// <param name="serverUrl">服務端處理Http請求的地址</param>param>
  62.         /// <param name="fileName">本機存放區的檔案名稱</param>
  63.         private void DownloadFile(string serverUrl, string fileName)     
  64.         {
  65.             //開啟上次下載的檔案或建立檔案
  66.             long lStartPos = 0;
  67.             System.IO.FileStream fs;
  68.             if (System.IO.File.Exists(fileName))
  69.             {
  70.                 fs = System.IO.File.OpenWrite(fileName);
  71.                 lStartPos = fs.Length;
  72.                 fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移動檔案流中的當前指標
  73.             }
  74.             else
  75.             {
  76.                 fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
  77.                 lStartPos = 0;
  78.             }
  79.             //開啟網路連接
  80.             try
  81.             {
  82.                 System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(serverUrl);
  83.                 request.KeepAlive = true;
  84.                 if (lStartPos > 0)
  85.                     request.AddRange((int)lStartPos); //設定Range值  
  86.                 System.Text.Encoding utf8 = System.Text.Encoding.Default;
  87.                 //向伺服器請求,獲得伺服器回應資料流
  88.                 System.IO.Stream ns = request.GetResponse().GetResponseStream();
  89.                 byte[] nbytes = new byte[1024];
  90.                 int nReadSize = 0;
  91.                 nReadSize = ns.Read(nbytes, 0, 1024);
  92.                 while (nReadSize > 0 && dlThreadContinue)
  93.                 {
  94.                     fs.Write(nbytes, 0, nReadSize);
  95.                     nReadSize = ns.Read(nbytes, 0, 1024);
  96.                 }
  97.                 //fs.Flush();
  98.                 fs.Close();
  99.                 ns.Close();
  100.                 Cursor.Current = Cursors.Default;
  101.                 MessageBox.Show("下載完成", "系統提示");
  102.             }
  103.             catch (Exception ex)
  104.             {
  105.                 Cursor.Current = Cursors.Default;
  106.                 fs.Close();
  107.                 MessageBox.Show("下載過程中出現錯誤:" + ex.ToString(), "系統提示");
  108.             }
  109.         }
  110.         private void menuItem1_Click(object sender, EventArgs e)
  111.         {
  112.             Application.Exit();
  113.         }
  114.         private void btnStop_Click(object sender, EventArgs e)
  115.         {
  116.             if (this.btnStop.Text == "暫停")
  117.             {
  118.                 this.dlThreadContinue = false;
  119.                 Cursor.Current = Cursors.Default;
  120.                 this.btnDownLoad.Enabled = true;
  121.                 this.btnStop.Enabled = false;
  122.             }                     
  123.         }      
  124.     }
  125. }

複製代碼

擴充資料:
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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.