進來的項目中要實作能夠在windows service中調用指定項目的連結頁面。由于通路頁面時候使用的是ie浏覽器或其他浏覽器,是以想起用webclient類。
如果隻想從特定的URI請求檔案,則使用WebClient,它是最簡單的.NET類,它隻用一兩條指令執行基本操作,.NET FRAMEWORK目前支援以http:、https和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類提供了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是非常一般的類,可以使用任意協定發送請求和接受相應,它不能處理特定于任何協定的任何特性。