欢迎来到山村网

C# WebClient类用法实例

2019-03-02 14:33:45浏览:145 来源:山村网   
核心摘要:  这篇文章主要介绍了C#WebClient类用法实例,本文讲解使用WebClient下载文件、OpenWriter打开一个流使用指定的方法将数据写入

  这篇文章主要介绍了C# WebClient类用法实例,本文讲解使用WebClient下载文件、OpenWriter打开一个流使用指定的方法将数据写入到uri以及上传文件示例,需要的朋友可以参考下

  进来的项目中要实现能够在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

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21OpenRead(string uri)

  #region 读取指定uri的html

  ///

  /// 读取指定uri的html

  ///

  ///

  ///

  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);

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19OpenWriter(string uri,string method)

  #region 打开一个流使用指定的方法将数据写入到uri

  ///

  /// 打开一个流使用指定的方法将数据写入到uri

  ///

  ///

  ///

  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

  ///

  /// 把本地文件上传到指定uri

  ///

  ///

  ///

  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 把数据缓冲区上载到指定资源

  ///

  /// 把数据缓冲区上载到指定资源

  ///

  ///

  ///

  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是非常一般的类,可以使用任意协议发送请求和接受相应,它不能处理特定于任何协议的任何特性。

(责任编辑:豆豆)
下一篇:

C#中使用WinRAR实现加密压缩及解压缩文件

上一篇:

C#中获取、生成随机数的三种方法

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com