WebClient用法小結(轉載)
如果只想從特定的URI請求文件,則使用WebClient,它是最簡單的.NET類,它只用一兩條命令執行基本操作,.NET FRAMEWORK目前支持以http:、https:、ftp:、和 file: 方案標識符開頭的 URI。
WebClient下載文件
使用webclient下載文件有兩種方法,具體使用哪一種方法取決於文件內容的處理方式,如果只想把文件保存到磁盤上,使用downloadfile()方法,此方法有兩個參數,即請求的uri和請求文件的的數據保存位置。
更常見的是,應用程序需要處理從web站點檢索的數據,為此要用到OpenRead方法,此方法返回一個Stream對象,然后,可以Stream對象從數據流提取到內存中。
示例:OpenRead(string uri);
1 #region 讀取指定uri的html
2 /// <summary>
3 /// 讀取指定uri的html
4 /// </summary>
5 /// <param name="sender"></param>
6 /// <param name="e"></param>
7 private void button4_Click(object sender, EventArgs e)
8 {
9 WebClient wc = new WebClient();
10 string uri = "http://127.0.0.1/rss/sina.aspx";
11 Stream stream = wc.OpenRead(uri);
12 StreamReader sr = new StreamReader(stream);
13 string strLine = "";
14 while ((strLine = sr.ReadLine()) != null)
15 {
16 this.listBox1.Items.Add(strLine);
17 }
18 sr.Close();
19 }
20 #endregion
示例:OpenWriter(string uri,string method);
1 #region 打開一個流使用指定的方法將數據寫入到uri
2 /// <summary>
3 /// 打開一個流使用指定的方法將數據寫入到uri
4 /// </summary>
5 /// <param name="sender"></param>
6 /// <param name="e"></param>
7 private void button1_Click(object sender, EventArgs e)
8 {
9 WebClient wc = new WebClient();
10 string uri = "http://192.168.0.35/cims30/rss.txt";
11 Stream stream = wc.OpenWrite(uri, "PUT");
12 StreamWriter sw = new StreamWriter(stream);
13 sw.WriteLine("HelloWorldHelloWorldHelloWorldHelloWorld");
14 sw.Flush();
15 sw.Close();
16 MessageBox.Show("OK");
17 }
18 #endregion
openwriter方法返回一個可寫的數據流,便於用戶把數據發送給uri,可以指定用戶把數據發送給主機的方法,默認是post,上例假定0.35的服務器上有一個可寫的目錄刺馬s,這段代碼是在該目錄下創建rss.txt文件,其內容為“HelloWorldHelloWorldHelloWorldHelloWorld”
WebClient上傳文件
WebClient類提供了UploadFile()和UploadData()方法,在需要投遞HTML窗體或上傳整個文件時候,就可以使用這兩個方法。Uploadfile()方法把文件上傳到指定的位置,其中文件名字已經給出,uploaddata()方法把字節數組提供的二進制數據上傳到指定的uri;
示例:
1 #region 把本地文件上傳到指定uri
2 /// <summary>
3 /// 把本地文件上傳到指定uri
4 /// </summary>
5 /// <param name="sender"></param>
6 /// <param name="e"></param>
7 private void button2_Click(object sender, EventArgs e)
8 {
9 WebClient wc = new WebClient();
10 string targetPath = "http://127.0.0.1/rss/Data Configuration.zip";
11 string sourcePath = "d:\\Data Configuration.zip";
12 this.label1.Text = string.Format("uploading {0} to {1}", targetPath, sourcePath);
13 byte[] bt = wc.UploadFile(targetPath, "PUT", sourcePath);
14 MessageBox.Show("OK");
15 }
16 #endregion
17
18
19 #region 把數據緩沖區上載到指定資源
20 /// <summary>
21 /// 把數據緩沖區上載到指定資源
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25 private void button3_Click(object sender, EventArgs e)
26 {
27 WebClient wc = new WebClient();
28 string targetPath = "http://127.0.0.1/rss/kaifeng.jpg";
29 string sourcePath = @"C:\test.jpg";
30 FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
31 byte[] bt = new byte[fs.Length];
32 fs.Read(bt, 0, bt.Length);
33 wc.UploadData(targetPath, "PUT", bt);
34 }
35 #endregion
webclient功能有限,特別是不能使用身份驗證證書,這樣,上傳數據時候問題出現,現在許多站點都不會接受沒有身份驗證的上傳文件。盡管可以給請求添加標題信息並檢查相應中的標題信息,但這僅限於一般意義的檢查,對於任何一個協議,webclient沒有具體支持,。這是由於webclient是非常一般的類,可以使用任意協議發送請求和接受相應,它不能處理特定於任何協議的任何特性。
-------------------------------------------------------------------------------------------------------------

