欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# WebClient類用法實(shí)例

 更新時間:2015年07月04日 11:49:30   投稿:junjie  
這篇文章主要介紹了C# WebClient類用法實(shí)例,本文講解使用WebClient下載文件、OpenWriter打開一個流使用指定的方法將數(shù)據(jù)寫入到uri以及上傳文件示例,需要的朋友可以參考下

進(jìn)來的項(xiàng)目中要實(shí)現(xiàn)能夠在windows service中調(diào)用指定項(xiàng)目的鏈接頁面。由于訪問頁面時候使用的是ie瀏覽器或其他瀏覽器,所以想起用webclient類。

如果只想從特定的URI請求文件,則使用WebClient,它是最簡單的.NET類,它只用一兩條命令執(zhí)行基本操作,.NET FRAMEWORK目前支持以http:、https和file:標(biāo)識符開頭的uri。

WebClient下載文件

使用webclient下載文件有兩種方法,具體使用哪一種方法取決于文件內(nèi)容的處理方式,如果只想把文件保存到磁盤上,使用downloadfile()方法,此方法有兩個參數(shù),即請求的uri和請求文件的的數(shù)據(jù)保存位置。

更常見的是,應(yīng)用程序需要處理從web站點(diǎn)檢索的數(shù)據(jù),為此要用到OpenRead方法,此方法返回一個Stream對象,然后,可以Stream對象從數(shù)據(jù)流提取到內(nèi)存中。

示例: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 打開一個流使用指定的方法將數(shù)據(jù)寫入到uri
    /// <summary>
    /// 打開一個流使用指定的方法將數(shù)據(jù)寫入到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方法返回一個可寫的數(shù)據(jù)流,便于用戶把數(shù)據(jù)發(fā)送給uri,可以指定用戶把數(shù)據(jù)發(fā)送給主機(jī)的方法,默認(rèn)是post,上例假定0.35的服務(wù)器上有一個可寫的目錄刺馬s,這段代碼是在該目錄下創(chuàng)建rss.txt文件,其內(nèi)容為“HelloWorldHelloWorldHelloWorldHelloWorld”

上傳文件

WebClient類提供了UploadFile()和UploadData()方法,在需要投遞HTML窗體或上傳整個文件時候,就可以使用這兩個方法。Uploadfile()方法把文件上傳到指定的位置,其中文件名字已經(jīng)給出,uploaddata()方法把字節(jié)數(shù)組提供的二進(jìn)制數(shù)據(jù)上傳到指定的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 把數(shù)據(jù)緩沖區(qū)上載到指定資源
    /// <summary>
    /// 把數(shù)據(jù)緩沖區(qū)上載到指定資源
    /// </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功能有限,特別是不能使用身份驗(yàn)證證書,這樣,上傳數(shù)據(jù)時候問題出現(xiàn),現(xiàn)在許多站點(diǎn)都不會接受沒有身份驗(yàn)證的上傳文件。盡管可以給請求添加標(biāo)題信息并檢查相應(yīng)中的標(biāo)題信息,但這僅限于一般意義的檢查,對于任何一個協(xié)議,webclient沒有具體支持,。這是由于webclient是非常一般的類,可以使用任意協(xié)議發(fā)送請求和接受相應(yīng),它不能處理特定于任何協(xié)議的任何特性。

相關(guān)文章

最新評論