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