WebClient用法小结(转载)

 

如果只想从特定的URI请求文件,则使用WebClient,它是最简单的.NET类,它只用一两条命令执行基本操作,.NET FRAMEWORK目前支持以http:、https:、ftp:、和 file: 方案标识符开头的 URI。

WebClient下载文件

使用webclient下载文件有两种方法,具体使用哪一种方法取决于文件内容的处理方式,如果只想把文件保存到磁盘上,使用downloadfile()方法,此方法有两个参数,即请求的uri和请求文件的的数据保存位置。

更常见的是,应用程序需要处理从web站点检索的数据,为此要用到OpenRead方法,此方法返回一个Stream对象,然后,可以Stream对象从数据流提取到内存中。

示例:OpenRead(string uri);

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

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

WebClient类提供了UploadFile()UploadData()方法,在需要投递HTML窗体或上传整个文件时候,就可以使用这两个方法。Uploadfile()方法把文件上传到指定的位置,其中文件名字已经给出,uploaddata()方法把字节数组提供的二进制数据上传到指定的uri;

示例:

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

-------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------
注:本文转载于http://www.cnblogs.com/hfliyi/archive/2012/08/21/2649892.html,感谢原文作者!

WebClient 下载文件的更多相关文章

  1. C#使用WebClient下载文件到本地目录

    C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...

  2. webclient下载文件 带进度条

    private void button1_Click(object sender, EventArgs e) { doDownload(textBox1.Text.Trim()); } private ...

  3. 使用WebClient下载文件到本地目录

    利用WebClient实现下载文件 调用 string url = "https://timgsa.baidu.com/timg?image&quality=80&size= ...

  4. WebClient下载文件

    public void DownDile(string url) { WebClient client = new WebClient(); string URLAddress = @"ht ...

  5. C# WebClient类上传和下载文件

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

  6. C#异步批量下载文件

    C#异步批量下载文件 实现原理:采用WebClient进行批量下载任务,简单的模拟迅雷下载效果! 废话不多说,先看掩饰效果: 具体实现步骤如下: 1.新建项目:WinBatchDownload 2.先 ...

  7. WebClient.DownloadFile(线程机制,异步下载文件)

    线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...

  8. WebClient异步下载文件

    namespace ConsoleAppSyncDownload{    class Program    { static void Main(string[] args)        {     ...

  9. [c#]WebClient异步下载文件并显示进度

    摘要 在项目开发中经常会用到下载文件,这里使用winform实现了一个带进度条的例子. 一个例子 using System; using System.Collections.Generic; usi ...

随机推荐

  1. 京东HBase平台进化与演进

    https://mp.weixin.qq.com/s/7_dxrqFWwIJxNtL9-xD6FA

  2. radioButon的使用

    界面: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android=& ...

  3. Docs-.NET-C#-指南-语言参考-关键字-值类型:enum

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:enum 1.返回顶部 1. enum(C# 参考) 2015/07/20 enum 关键字用于声明枚举,一种包含一组被称为枚 ...

  4. Flutter 中AlertDialog确认提示弹窗

    import 'package:flutter/material.dart'; import 'dart:async'; enum Action { Ok, Cancel } class AlertD ...

  5. Vue 及双向数据绑定 Vue事件 以及Vue中的ref获取dom节点

    <template> <div id="app"> <h2>{{msg}}</h2> <input type="te ...

  6. Oracle 自动生成的视图VM_NSO_1

    作者:Jerry 有时候优化sql的时候,在执行计划中看到有VM_NSO_X的视图,在Oracle定义中,可以吧NSO理解为nested subquery optimizing,功能就是把in转换为j ...

  7. SAP标准屏幕中字段描述增强

    转自:https://www.cnblogs.com/sap-ronny/p/7921817.html如果想要改变标准屏幕中的字段名称,如把物料主数据基本数据元素的名字改为我们想要的名字 . 1.首先 ...

  8. VS2010/VS2012/VS2015下openGL环境配置(转)

    按:按照下述博文,三个例子均成功. https://blog.csdn.net/so_geili/article/details/51685005 请仔细阅读每一个字. 为了学习<OpenGL超 ...

  9. 细聊Oracle通过ODBC数据源连接SQL Server数据库

    类似文章搜索引擎上有很多,内容大致相同,今天所谓细聊是因为我在借鉴这些文章时候走了些弯路,所以写此文,为自己备忘,同时如果能为初涉此处知识点的小伙伴提供些帮助就更好了,文章结尾处的一些扩展有一定实战意 ...

  10. (十四)访问标志 Access_flags

    一.概念 上一章节讲到了常量池,如下图,常量池之后便是访问标志acess_flags,占2个字节(u2). 二.例子 编写一个接口. public interface Test{ public fin ...