標籤:style http io os 使用 ar for 檔案 資料
Net2.0中新增了很多組件,WebClient就是其中一個,功能也很強大,今天拿WebClient做了一個小實驗,只用到了一些很簡單的功能就可以實現以前不好實現的功能,很方便。
簡單介紹一下WebClient:
WebClient 類提供向 URI 標識的任何本地、Intranet 或 Internet 資源發送資料以及從這些資源接收資料的公用方法。
WebClient 類使用 WebRequest 類提供對資源的訪問。WebClient 執行個體可以通過任何已向 WebRequest.RegisterPrefix 方法註冊的 WebRequest 子代訪問資料。
注意
預設情況下,.NET Framework 支援以 http:、https:、ftp:、和 file: 方案標識符開頭的 URI。
下面描述用於將資料上傳到資源的 WebClient 方法:
OpenWrite 檢索一個用於將資料發送到資源的 Stream。
OpenWriteAsync 檢索 Stream,它在不阻止調用線程的情況下將資料發送到資源。
UploadData 將位元組數組發送到資源,並返回包含任何響應的 Byte 數組。
UploadDataAsync 在不阻止調用線程的情況下,將 Byte 數組發送到資源。
UploadFile 將本地檔案發送到資源,並返回包含任何響應的 Byte 數組。
UploadFileAsync 在不阻止調用線程的情況下,將本地檔案發送到資源。
UploadValues 將 NameValueCollection 發送到資源,並返回包含任何響應的 Byte 數組。
UploadValuesAsync 在不阻止調用線程的情況下,將 NameValueCollection 發送到資源,並返回包含任何響應的 Byte 數組。
UploadString 在不阻止調用線程的情況下,將 String 發送到資源。
UploadStringAsync 在不阻止調用線程的情況下,將 String 發送到資源。
下面描述從資源下載資料的 WebClient 方法:
OpenRead 從資源以 Stream 的形式返回資料。
OpenReadAsync 在不阻止調用線程的情況下,從資源返回資料。
DownloadData 從資源下載資料並返回 Byte 數組。
DownloadDataAsync 在不阻止調用線程的情況下,從資源下載資料並返回 Byte 數組。
DownloadFile 從資源將資料下載到本地檔案。
DownloadFileAsync 在不阻止調用線程的情況下,將資料從資源下載到本地檔案。
DownloadString 從資源下載 String 並返回 String。
DownloadStringAsync 在不阻止調用線程的情況下,從資源下載 String。
您可以使用 CancelAsync 方法取消尚未完成的非同步作業。
預設情況下,WebClient 執行個體不發送可選的 HTTP 前序。如果您的請求需要可選前序,必須將該前序添加到 Headers 集合。例如,要在響應中保留查詢,必須添加使用者代理程式前序。此外,如果使用者代理程式標題丟失,伺服器可能返回 500(內部伺服器錯誤)。
在 WebClient 執行個體中,AllowAutoRedirect 設定為 true。
給繼承者的說明 衍生類別應調用 WebClient 的基類實現,以確保衍生類別按預期方式工作。
實現源碼為:
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Text;
7. using System.Windows.Forms;
8. using System.Net;
9. using System.IO;
10.
11. namespace wiindowsFormsApplication
12. {
13. public partial class Form1 : Form
14. {
15. public Form1()
16. {
17. InitializeComponent();
18. this.textBox1.Text = @"http://dl-sh-ocn-1.pchome.net/0d/bx/koomail50b8.rar";
19. }
20.
21. WebClient webClient = new WebClient();
22. private void btn_down_Click(object sender, EventArgs e)
23. {
24.
25. if (webClient.IsBusy)//是否存在進行中中的Web請求
26. {
27. webClient.CancelAsync();
28. }
29. //為webClient添加事件
30. webClient.DownloadProgressChanged +=new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
31. webClient.DownloadFileCompleted+=new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
32. //開始下載
33. webClient.DownloadFileAsync(new Uri(this.textBox1.Text), "aa.rar");
34. }
35.
36. private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
37. {
38. this.progressBar1.Value = e.ProgressPercentage;
39. this.lbl_pro.Text = e.ProgressPercentage.ToString() + "%";
40. this.lbl_detail.Text = string.Format("正在下載檔案,完成進度{0}/{1}(位元組)"
41. ,e.BytesReceived
42. ,e.TotalBytesToReceive);
43. }
44.
45. private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
46. {
47. if (e.Cancelled)
48. MessageBox.Show("下載被取消!");
49. else
50. MessageBox.Show("下載完成!");
51. }
52.
53. private void btn_cancel_Click(object sender, EventArgs e)
54. {
55. this.webClient.CancelAsync();
56. this.webClient.Dispose();
57. }
58. }
59. }
運行介面:
【引用】WebClient下載資料