直接上代码,包各位看客能用!!!

1.首先请求参数的封装

/// <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; }
}

  2.HttpWebRequest 封装

/// <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();
memoryStream.Close();
GC.Collect();
// 8.HttpWebRequest 组装
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
webRequest.AllowWriteStreamBuffering = false;
webRequest.Method = "POST";
webRequest.Timeout = 1800000;
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;
}
}
}
}
/// <summary>
/// 上传执行 方法
/// </summary>
/// <param name="parameter">上传文件请求参数</param>
public static string Execute2(UploadParameterType parameter)
{
// 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);
byte[] postBytes = new byte[] { };
using (MemoryStream memoryStream = new MemoryStream())
{
// 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.组装结束分界线数据体 到内存流中
//memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
// 6.获取二进制数据
postBytes = memoryStream.ToArray();
}
// 7.HttpWebRequest 组装
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
//对发送的数据不使用缓存【重要、关键】
webRequest.AllowWriteStreamBuffering = false;
webRequest.Method = "POST";
webRequest.Timeout = 1800000;
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
webRequest.ContentLength = postBytes.Length + parameter.UploadStream.Length+endBoundaryBytes.Length;
if (Regex.IsMatch(parameter.Url, "^https://"))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
// 8.组装文件流
byte[] buffer = new byte[1024 * 1024 * 1];
int size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
while (size > 0)
{
requestStream.Write(buffer, 0, size);
size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
}
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
requestStream.Close();
// 9.获取响应
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;
} }

  HttpUploadClient类中两个Execute2,参考网上,大都用第一个,如果上传小文件没问题,要是比较大(百兆以上)就会内存溢出,然后就用流方式。思路是一样的

3.调用示例:

            using (FileStream fs = new FileStream(@"C:\\test.zip", FileMode.Open, FileAccess.Read))
{
Dictionary<string, string> postParameter = new Dictionary<string, string>();
postParameter.Add("name", "heshang");
postParameter.Add("param", "1 2 3");
string result = HttpUploadClient.Execute(new UploadParameterType
{
Url = url,
UploadStream = fs,
FileNameValue = "test.zip",
PostParameters = postParameter
});
}

  4.接口服务后台接受请求时:

public IHttpActionResult Post()
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
string pyPath = HttpContext.Current.Request["name"];
string Params = HttpContext.Current.Request["params"];
file.SaveAs("C:\\test.zip");
return Ok("");
}

  5.完美解决大文件上传

C# HttpWebRequest 后台调用接口上传大文件以及其他参数的更多相关文章

  1. webapi接口上传大文件

    通过WebApi或者MVC模式的接口上传文件时,总数报错 413 Request Entity Too Large IIS 404 服务未找到 解决方法: 1. 在web.config文件下找到sys ...

  2. tp5+layui 实现上传大文件

    前言: 之前所写的文件上传类通常进行考虑的是文件的类型.大小是否符合要求条件.当上传大文件时就要考虑到php的配置和服务器的配置问题.之前简单的觉得只要将php.ini中的表单上传的 大小,单脚本执行 ...

  3. Web上传大文件的解决方案

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  4. HttpWebRequest上传多文件和多参数——整理

    1.整理HttpWebRequest上传多文件和多参数.较上一个版本,更具普适性和简易型.注意(服务方web.config中要配置)这样就可以上传大文件了 <system.webServer&g ...

  5. php上传大文件的解决方案

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  6. JS上传大文件的解决方案

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  7. Webupload+PHP上传大文件

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  8. java上传大文件解决方案

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在10G内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以10G来进行限制. 第一步: 前端修改 由于项目使用的是BJ ...

  9. php上传大文件

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

随机推荐

  1. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  2. Codeforces 939 D Love Rescue

    Love Rescue 题意:Valya 和 Tolya 是一对情侣, 他们的T恤和头巾上都有小写字母,但是女朋友嫌弃男朋友上T恤上的字不和她的头巾上的字一样,就很生气, 然后来了一个魔法师, 它可以 ...

  3. 【转 | 侵删】2D 绘图技术中的坐标系统与坐标变换

    本文介绍在 2D 绘图技术中的坐标系统和坐标变换的相关知识.同时介绍 Kity 在这方面提供的 API .希望这些知识对于需要进行图形应用开发的同学会有所帮助. 锤子的故事 很久以前,有一个画家,他很 ...

  4. codeforces 456 E. Civilization(并查集+数的直径)

    题目链接:http://codeforces.com/contest/456/problem/E 题意:给出N个点,M条边,组成无环图(树),给出Q个操作,操作有两种: 1 x,输出x所在的联通块的最 ...

  5. 【Offer】[62] 【圆圈中最后剩下的数字】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 牛客网刷题地址 ...

  6. 【Offer】[56-2] 【数组中唯一只出现一次的数字】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 在一个数组中除一个数字只出现一次之外,其他数字都出现了三次.请找出那个只出现一次的数字 [牛客网刷题地址]无 思路分析 如果一个数字出现 ...

  7. Linux-配置YUM仓库

    范例:配置Yum仓库 Yum软件仓库的作用是为了进一步简化RPM管理软件的难度以及自动分析所需软件包及其依赖关系的技术.可以把Yum想象成是一个硕大的软件仓库,里面保存有几乎所有常用的工具,而且只需要 ...

  8. Python(Head First)学习笔记:六

    6 定制数据对象:数据结构自定义 打包代码与数据 james2.txt: James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-2 ...

  9. Java中各种引用(Reference)解析

    目录 1,引用类型 2, FinalReference 2.1, Finalizer 3, SoftReference 4, WeakReference 5, PhantomReference 6, ...

  10. 超级密码(BFS)

    Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进 ...