HttpWebRequest上传多文件和多参数——整理
1.整理HttpWebRequest上传多文件和多参数。较上一个版本,更具普适性和简易型。注意(服务方web.config中要配置)这样就可以上传大文件了
<system.webServer>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" />
</requestFiltering>
</system.webServer>
<system.web>
<httpRuntime maxRequestLength="2048000"/>
</system.web>
2.HttpWebRequest的封装
/// <summary>
/// Http上传文件类 - HttpWebRequest封装
/// </summary>
public class HttpUploadClient
{
public static string ExecuteMultipartRequest(string url, List<KeyValue> nvc)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url, UriKind.RelativeOrAbsolute));
webRequest.Method = "POST";
webRequest.Timeout = 1800000;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
webRequest.KeepAlive = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
using (var rs = webRequest.GetRequestStream())
{
// 普通参数模板
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
//带文件的参数模板
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
foreach (KeyValue keyValue in nvc)
{
//如果是普通参数
if (keyValue.FilePath == null)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, keyValue.Key, keyValue.Value);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
//如果是文件参数,则上传文件
else
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string header = string.Format(headerTemplate, keyValue.Key, keyValue.Value, keyValue.ContentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
using (var fileStream = new FileStream(keyValue.FilePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[4096];
int bytesRead = 0;
long total = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
total += bytesRead;
}
}
} }
byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
} // 获取响应
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string body = reader.ReadToEnd();
reader.Close();
return body;
}
}
}
3.参数封装(支持普通参数和多文件)
public class KeyValue
{
public string Key;
public string Value;
public string FilePath;
public string ContentType = "*/*";
public KeyValue(string key, string value, string filePath, string contentType)
{
Key = key;
Value = value;
FilePath = filePath;
ContentType = contentType;
}
public KeyValue() { }
public KeyValue(string key, string value, string filePath)
{
Key = key;
Value = value;
FilePath = filePath;
}
public KeyValue(string key, string value)
{
Key = key;
Value = value;
}
}
4.调用示例
List<KeyValue> keyValues = new List<KeyValue>();
keyValues.Add(new KeyValue("key1", "12_423232523"));
keyValues.Add(new KeyValue("key2", " 1 2 3"));
keyValues.Add(new KeyValue("file1", "1.img", @"C:\临时\tempDB\1.img"));
keyValues.Add(new KeyValue("file2", "2.xls", @"C:\临时\tempDB\2.xls"));
HttpUploadClient.ExecuteMultipartRequest(url, keyValues);
5.服务接收方示例
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
{
HttpPostedFile file = HttpContext.Current.Request.Files[i];
file.SaveAs(""+file.FileName);
}
string Params = HttpContext.Current.Request.Form["key1"];
HttpWebRequest上传多文件和多参数——整理的更多相关文章
- C# HttpWebRequest 后台调用接口上传大文件以及其他参数
直接上代码,包各位看客能用!!! 1.首先请求参数的封装 /// <summary> /// 上传文件 - 请求参数类 /// </summary> public class ...
- HTTP上传大文件的注意点
使用HttpWebRequest上传大文件时,服务端配置中需要进行以下节点配置: <system.web> <compilation debug="true" t ...
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
原文地址:http://docode.top/Article/Detail/10002 目录: 1.Http协议上传文件(以图片为例)请求报文体内容格式 2.完整版HttpWebRequest模拟上传 ...
- C#在WinForm下使用HttpWebRequest上传文件
转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...
- 关于HttpWebRequest上传文件
我们web 操作离不开 http请求响应 HttpWebRequest上传文件也是一样的道理 下面码一些代码: private void UploadFile(string strRequestUri ...
- [转]C#在WinForm下使用HttpWebRequest上传文件并显示进度
/// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param name=&qu ...
- web大文件上传控件-设置附加参数-Xproer.HttpUploader6
自定义附加字段在up6.js中定义,也可以不用定义: 注意: 1.附加字段必须是字符串类型. 2.如果附加字段的值包含中文,在上传前必须使用encodeURIComponent进行编码. 在引 ...
- PHP代码中使用post参数上传大文件
今天连续碰到了两个同事向我反应上传大文件(8M)失败的事情! 都是在PHP代码中通常使用post参数进行上传文件时,当文件的大小大于8M时,上传不能不成功. 首先,我想到了nginx的client_m ...
- 对于Nginx+PHP实现大文件上传时候需要修改的参数
post_max_size表示POST表单提交的最大大小upload_max_filesize 表示文件上传的最大大小. 通常post_max_size设置的值必须必upload_max_filesi ...
随机推荐
- HttpClient 三种 Http Basic Authentication 认证方式,你了解了吗?
Http Basic 简介 HTTP 提供一个用于权限控制和认证的通用框架.最常用的 HTTP 认证方案是 HTTP Basic authentication.Http Basic 认证是一种用来允许 ...
- 从Linux服务器下载上传文件
首先要确定好哪两种的连接:Linux常用的有centors和unbantu两种版本,PC端Mac和Windows 如果在两个Linux之间传输,或Linux和Mac之间传输可以使用scp命令,类似于s ...
- fdfsdf
名称:字符串 来源:2019年陕西省选 题目内容 传送门 洛谷(P5392) 题目描述 给出一个长度为$n$的由小写字母组成的字符串$a$,设其中第$i$个字符为$a_i(1≤i≤n)$. 设删掉第$ ...
- python修改内存,(修改植物大战僵尸)
import win32process # 进程模块 import win32con # 系统定义 import win32api # 调用系统模块 import ctypes # c语言类型 imp ...
- 线段树离散化 unique + 二分查找 模板 (转载)
离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率. 通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小.例如: 原数据:1,999,100000,15:处理 ...
- CodeForces 85D Sum of Medians Splay | 线段树
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...
- NOIP 2005 等价表达式 题解
题意 给一个表达式然后再给n个表达式,判断是否等价 一道大模拟题,将a带为数,并且取模防止溢出 #include<bits/stdc++.h> using namespace std; c ...
- 049 模块6-wordcloud库的使用
目录 一.wordcloud库基本介绍 1.1 wordcloud库概述 1.2 wordcloud库的安装 二.wordcloud库使用说明 2.1 wordcloud库基本使用 2.2 wordc ...
- Vert.x学习之 Web Client
Vert.x Web Client 原文档 组件源码 组件示例 中英对照表 Pump:泵(平滑流式数据读入内存的机制,防止一次性将大量数据读入内存导致内存溢出) Response Codec:响应编解 ...
- 策略模式+注解 干掉业务代码中冗余的if else...
前言: 之前写过一个工作中常见升级模式-策略模式 的文章,里面讲了具体是怎样使用策略模式去抽象现实中的业务代码,今天来拿出实际代码来写个demo,这里做个整理来加深自己对策略模式的理解. 一.业务 ...