【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
原文地址:http://docode.top/Article/Detail/10002
目录:
2、完整版HttpWebRequest模拟上传文件请求报文内容封装
3、asp.net(c#)使用HttpWebRequest携带请求参数模拟上传文件封装源码下载
一、Http协议上传文件(以图片为例)请求报文体内容格式
首先,我们来看下通过浏览器上传文件的请求报文内容格式,这里以本人自己写的实例为例,如下图。除了能上传图片(即:头像字段),还携带了用户名、密码两个字段,很好的诠释了http带参数上传文件的情形。点击提交按钮后,浏览器会将文件(即头像文件)二进制数据和用户名、密码以post方式发送至服务器。这时我们可以通过抓包工具(如:fiddler)(或者浏览器自带的开发者工具F12)查看请求报文内容。

通过抓包工具获取到携带参数上传文件请求报文体内容格式如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
POST /PostUploadHandler.ashx HTTP/1.1Host: localhost:44187Connection: keep-aliveContent-Length: 19839Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: http://localhost:44187Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryNSF3vGLxKBlk5kcBReferer: http://localhost:44187/UploadDemo.aspxAccept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8------WebKitFormBoundaryNSF3vGLxKBlk5kcBContent-Disposition: form-data; name="userName"admin------WebKitFormBoundaryNSF3vGLxKBlk5kcBContent-Disposition: form-data; name="userPwd"123456------WebKitFormBoundaryNSF3vGLxKBlk5kcBContent-Disposition: form-data; name="photo"; filename="1.png"Content-Type: image/png<!--这一行是文件二进制数据-->------WebKitFormBoundaryNSF3vGLxKBlk5kcB-- |
1、请求头中有一个Content-Type参数(默认值:application/x-www-form-urlencoded),其中multipart/form-data值表示向服务器发送二进制数据,boundary表示请求体的分界线,服务器就是依靠分界线分割请求体来读取数据,此参数值可自定义。
2、请求体依靠boundary有规则的排列参数。每一行字符串后面包含一个换行符“\r\n”,有一个开始分界线(--boundary)和一个结束分界线(--boundary--),参数与参数之间通过--boundary分离,每一个参数的键(key)和值(value)之间包含一个空行即:“\r\n"。
二、完整版HttpWebRequest模拟上传文件请求报文内容封装
通过上面介绍,我们已经清楚了解了http协议上传文件的POST请求报文内容格式,在.net中使用HttpWebRequest上传文件,我们只要按照此格式封装请求报文,即可实现携带参数上传功能了。
为了方便扩展和维护,把所有请求参数(如上传地址url、携带参数、上传文件流等)封装到一个类中,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/// <summary>/// 上传文件 - 请求参数类/// </summary>public class UploadParameterType{ public UploadParameterType() { FileNameKey = "fileName"; Encoding = Encoding.UTF8; PostParameters = new Dictionary<string, string>(); } /// <summary> /// 上传地址 /// </summary> public string Url { get; set; } /// <summary> /// 文件名称key /// </summary> public string FileNameKey { get; set; } /// <summary> /// 文件名称value /// </summary> public string FileNameValue { get; set; } /// <summary> /// 编码格式 /// </summary> public Encoding Encoding { get; set; } /// <summary> /// 上传文件的流 /// </summary> public Stream UploadStream { get; set; } /// <summary> /// 上传文件 携带的参数集合 /// </summary> public IDictionary<string, string> PostParameters { get; set; } } |
新建一个上传文件工具类(命名为:HttpUploadClient),在类中增加上传方法(命名为:Execute),如下所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/// <summary>/// Http上传文件类 - HttpWebRequest封装/// </summary>public class HttpUploadClient{ /// <summary> /// 上传执行 方法 /// </summary> /// <param name="parameter">上传文件请求参数</param> public static string Execute(UploadParameterType parameter) { } static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }} |
Post上传请求体参数是二进制格式的,我们只需要将参数根据以上报文体内容格式拼接好数据,存放在内存流里面,拼接完整后,将整个内存流转换成二进制格式写入到HttpWebRequest请求体中就行,下面我们来一步一步的拼接报文体内容。
1、定义开始结束分界线boundary及拼接开始分界线:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static string Execute(UploadParameterType parameter){ using (MemoryStream memoryStream = new MemoryStream()) { // 1.分界线 string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), // 分界线可以自定义参数 beginBoundary = string.Format("--{0}\r\n", boundary), endBoundary = string.Format("\r\n--{0}--\r\n", boundary); byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary), endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary); // 2.组装开始分界线数据体 到内存流中 memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length); // …… }} |
2、拼接附加携带参数:
|
1
2
3
4
5
6
7
8
9
10
11
|
// 3.组装 上传文件附加携带的参数 到内存流中if (parameter.PostParameters != null && parameter.PostParameters.Count > 0){ foreach (KeyValuePair<string, string> keyValuePair in parameter.PostParameters) { string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary); byte[] parameterHeaderBytes = parameter.Encoding.GetBytes(parameterHeaderTemplate); memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length); }} |
3、拼接上传文件体及结束分界线boundary(需要注意的是Content-Type的值是:application/octet-stream):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// 4.组装文件头数据体 到内存流中string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", parameter.FileNameKey, parameter.FileNameValue);byte[] fileHeaderBytes = parameter.Encoding.GetBytes(fileHeaderTemplate);memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);// 5.组装文件流 到内存流中byte[] buffer = new byte[1024 * 1024 * 1];int size = parameter.UploadStream.Read(buffer, 0, buffer.Length);while (size > 0){ memoryStream.Write(buffer, 0, size); size = parameter.UploadStream.Read(buffer, 0, buffer.Length);}// 6.组装结束分界线数据体 到内存流中memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); |
4、通过以上步骤,上传文件请求体内容数据已经拼接完成,接下来就是对HttpWebRequest对象的属性设置(如:请求地址Url,请求方法Method,Content-Type等),把整个上传文件请求体内存流写入到HttpWebRequest对象的请求体中,然后发起上传请求。如下源码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// 7.获取二进制数据byte[] postBytes = memoryStream.ToArray();// 8.HttpWebRequest 组装HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));webRequest.Method = "POST";webRequest.Timeout = 10000;webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);webRequest.ContentLength = postBytes.Length;if (Regex.IsMatch(parameter.Url, "^https://")){ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;}// 9.写入上传请求数据using (Stream requestStream = webRequest.GetRequestStream()){ requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close();}// 10.获取响应using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()){ using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), parameter.Encoding)) { string body = reader.ReadToEnd(); reader.Close(); return body; }} |
完整版HttpWebRequest模拟上传文件代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/// <summary>/// Http上传文件类 - HttpWebRequest封装/// </summary>public class HttpUploadClient{ /// <summary> /// 上传执行 方法 /// </summary> /// <param name="parameter">上传文件请求参数</param> public static string Execute(UploadParameterType parameter) { using (MemoryStream memoryStream = new MemoryStream()) { // 1.分界线 string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), // 分界线可以自定义参数 beginBoundary = string.Format("--{0}\r\n", boundary), endBoundary = string.Format("\r\n--{0}--\r\n", boundary); byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary), endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary); // 2.组装开始分界线数据体 到内存流中 memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length); // 3.组装 上传文件附加携带的参数 到内存流中 if (parameter.PostParameters != null && parameter.PostParameters.Count > 0) { foreach (KeyValuePair<string, string> keyValuePair in parameter.PostParameters) { string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary); byte[] parameterHeaderBytes = parameter.Encoding.GetBytes(parameterHeaderTemplate); memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length); } } // 4.组装文件头数据体 到内存流中 string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", parameter.FileNameKey, parameter.FileNameValue); byte[] fileHeaderBytes = parameter.Encoding.GetBytes(fileHeaderTemplate); memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length); // 5.组装文件流 到内存流中 byte[] buffer = new byte[1024 * 1024 * 1]; int size = parameter.UploadStream.Read(buffer, 0, buffer.Length); while (size > 0) { memoryStream.Write(buffer, 0, size); size = parameter.UploadStream.Read(buffer, 0, buffer.Length); } // 6.组装结束分界线数据体 到内存流中 memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); // 7.获取二进制数据 byte[] postBytes = memoryStream.ToArray(); // 8.HttpWebRequest 组装 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute)); webRequest.Method = "POST"; webRequest.Timeout = 10000; webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); webRequest.ContentLength = postBytes.Length; if (Regex.IsMatch(parameter.Url, "^https://")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } // 9.写入上传请求数据 using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); } // 10.获取响应 using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), parameter.Encoding)) { string body = reader.ReadToEnd(); reader.Close(); return body; } } } } static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }} |
为了验证封装是否正确,可以写一个控制台应用程序来模拟Http协议上传文件(以图片为例),结果如图:

三、asp.net(c#)使用HttpWebRequest携带请求参数模拟上传文件封装源码下载
扫一扫获取百度网盘超级vip账号


【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端的更多相关文章
- ASP.NET上传大文件出现网页无法显示的问题
使用FileUpload上传的时候,默认允许大小是4M,而当小于4M的时候正常运行:当超过4M将显示网页无法显示.解决方法如下: 在web.config中的<system.web>< ...
- [Asp.net]Uploadify上传大文件,Http error 404 解决方案
引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章:[Asp.net]Upl ...
- ASP.NET上传大文件的问题
原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...
- ASP.NET 使用 plupload 上传大文件时出现“blob”文件的Bug
最近在一个ASP.NET 项目中使用了plupload来上传文件,结果几天后客户发邮件说上传的文件不对,说是文件无法打开 在进入系统进行查看后发现上传的文件竟然没有后缀,经过一番测试发现如果文件上传的 ...
- [Asp.net]Uploadify上传大文件,Http error 404 解决方案 - wolfy
引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章: [Asp.net]Up ...
- Asp.Net上传大文件带进度条swfupload
Asp.Net基于swfupload上传大文件带进度条百分比显示,漂亮大气上档次,大文件无压力,先看效果 一.上传效果图 1.上传前界面:图片不喜欢可以自己换 2.上传中界面:百分比显示 3.上传后返 ...
- asp.net core流式上传大文件
asp.net core流式上传大文件 首先需要明确一点就是使用流式上传和使用IFormFile在效率上没有太大的差异,IFormFile的缺点主要是客户端上传过来的文件首先会缓存在服务器内存中,任何 ...
- C# HttpWebRequest 后台调用接口上传大文件以及其他参数
直接上代码,包各位看客能用!!! 1.首先请求参数的封装 /// <summary> /// 上传文件 - 请求参数类 /// </summary> public class ...
- C# Asp.NET实现上传大文件(断点续传)
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
随机推荐
- HDFS DataNode 设计实现解析
前文分析了 NameNode,本文进一步解析 DataNode 的设计和实现要点. 文件存储 DataNode 正如其名是负责存储文件数据的节点.HDFS 中文件的存储方式是将文件按块(block)切 ...
- 玩转JavaScript OOP[4]——实现继承的12种套路
概述 在之前的文章中,我们借助构造函数实现了"类",然后结合原型对象实现了"继承",并了解了JavaScript中原型链的概念. 理解这些内容,有助于我们更深入 ...
- Lesson 8 The best and the worst
Text Joe Sanders has the most beautiful garden in our town. Nearly everbody enters for 'The Nicest G ...
- CI-持续集成(1)-软件工业“流水线”概述
CI-持续集成(1)-软件工业“流水线”概述 1 概述 持续集成(Continuous integration)是一种软件开发实践,即团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次, ...
- 玩转Windows服务系列——服务运行、停止流程浅析
通过研究Windows服务注册卸载的原理,感觉它并没有什么特别复杂的东西,Windows服务正在一步步退去它那神秘的面纱,至于是不是美女,大家可要睁大眼睛看清楚了. 接下来研究一下Windows服务的 ...
- 久违的问候-----eclipse中搭建maven项目2016年
好久没有写过博客了,可是一直向别人推荐自己的博客,深感惭愧!今天再次在寒冷之夜继续code,config,write. 接下来,我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一 ...
- Golang与C#之switch区别
Golang与C#之switch区别 Go是Google开发的一种编译型,可并行化,并具有垃圾回收功能的编程语言. C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序 ...
- appledoc 使用brew命令安装使用
appledoc --project-name yushuyi12345677 --project-company "xiaoyu123" --company-id aaaa -- ...
- [Hadoop大数据]——Hive初识
Hive出现的背景 Hadoop提供了大数据的通用解决方案,比如存储提供了Hdfs,计算提供了MapReduce思想.但是想要写出MapReduce算法还是比较繁琐的,对于开发者来说,需要了解底层的h ...
- AMD规范与CMD规范的区别
AMD规范与CMD规范的区别是什么? 在比较之前,我们得先来了解下什么是AMD规范?什么是CMD规范?当然先申明一下,我个人也是总结下而已,也是网上看到的资料,自己总结下或者可以说整理下而已,供 ...