一、概述

System.Net.WebClient属于高层类、使用简单。均支持异步版本。支持http,https,fpt,files等URI。

建议不要将 WebClient 类用于新的开发。Net4.5及以上请改用 System.Net.Http.HttpClient 类。

二、下载

1、OpenRead:打开一个可读的Stream。

对于FTP资源,默认使用RETR命令;对于HTTP资源,默认使用Get方法。

Stream= client.OpenRead(serverUri):

举例

WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Encoding = Encoding.GetEncoding("gb2312");
client.Credentials = new NetworkCredential("csp", "welcome"); Stream data = client.OpenRead(url);//OpenRead为下载的数据打开一个只读的流
StreamReader reader = new StreamReader(data, Encoding.GetEncoding("gb2312"));
sting s = reader.ReadToEnd();
reader.Close();
return s;

2、DownloadData:以byte[]形式下载资源。

byte[] data= client.DownloadData(serverUri):

3、DownloadFile:将资源下载在本地文件。

void client.DownloadFile(serverUri,localFile):

4、DownloadString:以string的形式下载资源。

string content =client.DownloadString(serverUri):

5、事件

  1. DownloadProgressChanged事件:
  2. Download***Completed事件

6、获取下载网址的真实文件名

获取http头部信息的内容。

Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。

Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
形如:”Content-Disposition: attachment;filename=FileName.txt“。

当你在响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着你不想直接显示内容,而是弹出一个"文件下载"的对话框

WebClient client = new WebClient();
byte[] data = client.DownloadData(fileUrl);
var mc = Regex.Matches(Server.UrlDecode(client.ResponseHeaders["Content-Disposition"]), @"filename=(.+)");
string filename = mc[0].Groups[1].Value;

三、上传

1、OpenWrite:打开一个可写的流。

对于FTP资源,默认使用STOR命令;对于HTTP资源,默认使用POST方法。

Strean =client.OpenWrite(serverUri);

2、UploadData:将byte[]数据上传到serverUri。

byte[] =client.UploadData(serverUri,byte[]);

3、UploadFile:将本地文件上传到serverUri。

byte[] =client.UploadFile(serverUri,localFile);

举例:

WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Credentials = new NetworkCredential("csp", "welcome");
client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompletedCallback);
client.UploadFileAsync(new Uri(uriString + Path.GetFileName(localfileName)), null, localfileName, progressbarfrom);

/// <summary>
/// 上传过程处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
(e.UserState as ProgressBar).Value = e.ProgressPercentage;
} private static void UploadFileCompletedCallback(object sender, UploadFileCompletedEventArgs e)
{
if (e.Error == null)
MessageBox.Show("完成上传");
else
throw e.Error;
}

4、UploadValues:将指定的名/值集合到serverUri。

byte[] =client.UploadValues(serverUri,namevalueCollection);

5、事件:

  1. UploadProgressChanged:e.ProcessPercentage,e.TatalBytesToReceive,e.BytesReceived;
  2. Upload***Completed: e.Cancelled,e.Error,E.UserState;

四、System.Url类(统一资源表示符)

Uri url=new Uri(”http://www.mi.com:2165/somFolder/someFile.htm?order=true”);

  • OriginalString    获取传递给 Uri 构造函数的原始 URI 字符串。http://www.mi.com:2165/somFolder/someFile.htm?order=true
  • Scheme    获取此 URI 的方案名称。http
  • IsFile    获取一个值,该值指示指定的 Uri 是否为文件 URI。false
  • Host    获取此实例的主机部分。www.mi.com
  • HostNameType    获取 URI 中指定的主机名的类型。DNS
  • Port    获取此 URI 的端口号。2105
  • IsDefaultPort    获取一个值,该值指示 URI 的端口值是否为此方案的默认值。false
  • AbsolutePath    获取 URI 的绝对路径。/somFolder/someFile.htm
  • Query    获取指定 URI 中包括的任何查询信息。?order=true
  • PathAndQuery    获取用问号 (?) 分隔的 AbsolutePath 和 Query 属性。/somFolder/someFile.htm?order=true

78、WebClient实现上传下载 System.Net、System.Uri类的更多相关文章

  1. C# WebClient 实现上传下载网络资源

    下载数据 WebClient wc = new WebClient();1 string str= wc.DownloadString("地址")://直接下载字符串 2 wc.D ...

  2. WebClient上传下载文件,小白篇

    WebClient的上传文件一直报错,各种百度各种稀奇古怪的东西,终于百度到一篇小白学习篇 转自: https://www.cnblogs.com/cncc/p/5722231.html 使用C#We ...

  3. webclient上传下载文件

    定义WebClient使用的操作类: 操作类名称WebUpDown WebClient上传文件至Ftp服务: //// <summary> /// WebClient上传文件至Ftp服务 ...

  4. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  5. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  6. Linux 终端访问 FTP 及 上传下载 文件

    今天同事问我一个问题,在Linux 下访问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上.google 一下. 方 ...

  7. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

  8. Linux 终端訪问 FTP 及 上传下载 文件

    今天同事问我一个问题,在Linux 下訪问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上. google 一下. ...

  9. HttpClient文件上传下载

    1 HTTP HTTP 协议可能是如今 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序须要直接通过 HTTP 协议来訪问网络资源. 尽管在 JDK 的 java.net ...

随机推荐

  1. mysql 索引数据结构及原理

    原文:http://www.uml.org.cn/sjjm/201107145.asp 1 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子 ...

  2. *2_3_5_加入reference model

    摘自:http://book.2cto.com/201408/46009.html 在2.1节中讲述验证平台的框图时曾经说过,reference model用于完成和DUT相同的功能. referen ...

  3. python实例:快速找出多个字典中的公共键

    1.生成随机字典 # 从abcdefg 中随机取出 3-6个,作为key, 1-4 的随机数作为 value s1 = {x : randint(1, 4) for x in sample('abcd ...

  4. IIS 站点 共享目录

    1.先建立站点,再设置文件夹为共享,Everyone 2.Mac电脑 Everyone不能访问,必须建立用户

  5. Quartz大致介绍(一)

    1. 介绍 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预先确定(被纳 ...

  6. 基于easyUI实现经典系统主界面

    此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. 相关文件介绍 1. home.jsp:系统主界面 <!DOCTYPE html PUBLIC "-//W3C ...

  7. Django请求生命周期之响应内容

    Django请求生命周期: 1.发送http请求2.服务器接受,根据请求头中的url在路由关系表中进行匹配(从上到下)3.匹配成功后,执行指定的views函数 URL -> 函数 ==>F ...

  8. ORA-16014: log 3 sequence# 540 not archived, no available destinations

    https://blog.csdn.net/zonelan/article/details/7329369

  9. D3.js 入门教程

    最近需要用到d3, 记录下d3的教程 网上搜了几个关于d3的教程 D3.js 入门教程      http://wiki.jikexueyuan.com/project/d3wiki/author.h ...

  10. Codeforces Round #419 A+B

    A. Karen and Morning time limit per test  2 seconds memory limit per test  512 megabytes   Karen is ...