WebClient 用法小结

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

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

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

 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;

示例:

上传文件
#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

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

时间: 2024-08-07 03:46:20

WebClient 用法小结的相关文章

WebClient用法小结(转载)

如果只想从特定的URI请求文件,则使用WebClient,它是最简单的.NET类,它只用一两条命令执行基本操作,.NET FRAMEWORK目前支持以http:.https:.ftp:.和 file: 方案标识符开头的 URI. WebClient下载文件 使用webclient下载文件有两种方法,具体使用哪一种方法取决于文件内容的处理方式,如果只想把文件保存到磁盘上,使用downloadfile()方法,此方法有两个参数,即请求的uri和请求文件的的数据保存位置. 更常见的是,应用程序需要处理

C++ typedef用法小结 (※不能不看※)

C++ typedef用法小结 (※不能不看※) 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针, // 和一个字符变量:以下则可行:typedef char* PCHAR; // 一般用大写PCHAR pa, pb; // 可行,同时声明了两个指向字符变量的指针虽然:char *pa, *pb;也可行,但相对来说没有用typedef的形式直观,

英语语法最终珍藏版笔记- 21it 用法小结

it 用法小结 it 在英语中的意思较多,用法较广,现总结如下. 一.it作句子的真正主语 1.it 指前面已经提到过的人或事物,有时指心目中的或成为问题的人或事物,作真正主语. 例如: What’s this? -It is a sheep? 这是什么??这是一只绵羊. Who is it? -It’s me (I). 谁??是我. It’s the wind shaking the window. 是风刮得窗户响. 2.it指时间.季节.一般用在无人称动词的主语. 例如: What time

iOS开发系列之一 - UIButton 用法小结

// 初始化按钮并设置类型 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 能够定义的UIButton类型有以下6种: // typedef enum { // UIButtonTypeCustom = 0, 自定义风格 // UIButtonTypeRoundedRect, 圆角矩形 // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用 // UIButto

iOS开发系列之二 - UILabel 用法小结

// 初始化标签 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)]; // 设置标签文字 label.text = @"This is a test text.This is a test text.This is a test text."; // 设置标签文字字体 // 使用系统字体 label.font = [UIFont systemFontOfSize:20]; //

jstree用法小结

jstree是一款功能强大的插件.官网地址http://www.jstree.com/ $('#jstree').jstree({ "core" : { "animation" : 0, "themes" : { "dots": true,"icons":true ,"stripes":false}, "check_callback" : true, "mu

结构体定义 typedef struct 用法详解和用法小结

typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请node 的变量时,需要这样写,struct node n;若用typedef,可以这样写,typedef struct node{}NODE; .在申请变量时就可以这样写,NODE n;区别就在于使用时,是否可以省去struct这个关键字. 第三篇:struct和typedef struct 分三块来讲述:1 首先:在C中定义一个结构体

asp.net中Page.ClientScript.RegisterStartupScript用法小结(转)

//ASP.NET后台页面跳转 Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>if(confirm('保存成功!是否继续添加?')){location.href='ProductonAdd.aspx'}else{location.href='ProductonList.aspx'}</script>"); //后台弹出确定框 ClientScript.

Java中static、final用法小结(转)

一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引用不可再变.其初始化可以在两个地方,一是其定义处,也就是说在final变量定义时直接给其赋值,二是在构造函数中.这两个地方只能选其一,要么在定义时给值,要么在构造函数中给值,不能同时既在定义时给了值,又在构造函数中给另外的值. 当函数参数为final类型时,你可以读取使用该参数,但是无法改变该参数的