c# webclient类用法实例
这篇文章主要介绍了c#webclient类用法实例,本文讲解使用webclient下载文件、openwriter打开一个流使用指定的方法将数据写入到uri以及上传文件示例,需要的朋友可以参考下进来的项目中要实现能够在windowsservice中调用指定项目的链接页面。由于访问页面时候使用的是ie浏览器或其他浏览器,所以想起用webclient类。
如果只想从特定的uri请求文件,则使用webclient,它是最简单的.net类,它只用一两条命令执行基本操作,.netframework目前支持以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.read(bt, 0, bt.length);
wc.uploaddata(targetpath, "put", bt);
}
#endregion
webclient功能有限,特别是不能使用身份验证证书,这样,上传数据时候问题出现,现在许多站点都不会接受没有身份验证的上传文件。尽管可以给请求添加标题信息并检查相应中的标题信息,但这仅限于一般意义的检查,对于任何一个协议,webclient没有具体支持,。这是由于webclient是非常一般的类,可以使用任意协议发送请求和接受相应,它不能处理特定于任何协议的任何特性。
更多技术文章信息请查看: 技术文章 我在顶贴~!~ 哎 我我我怎么办啊?? 我不是来为楼主呐喊加油的,也不是对楼主进行围堵攻击的。 其实今天baidu 又 抽风了 楼主,你要继续努力啊!你是bbs的希望啊!你是网络文学的希望啊!你是整个网络界的希望文学界的希望啊!你是整个人类的希望啊!你是整个太阳系的希望啊!你是整个异次元空间的希望啊! 怎么就没人拜我为偶像那?? 哎 我我我怎么办啊?? 这就是我斗胆的一点粗略分析,每天睡觉以前,我都会把您的帖子再三拜读,拜读。 去干吗啊~~~伤心啊~~~
页:
[1]