post提交表单一般无非是一般text文本和文件类型,如下

<input type="file"/>
<input type="text"/>

如果模拟post提交表单的过程(类似于HTML表单提交),该怎么做呢

这里就需要用到HttpClietn类

首先我们需要一个类去包装这些需要上载的数据,例如

   /// <summary>
/// 包装Data数据的Model
/// </summary>
public class SendData
{
/// <summary>
/// 多个文件的
/// </summary>
public List<HttpPostedFileBase> FileList { get; set; } /// <summary>
/// 单个文件
/// </summary>
public HttpPostedFileBase File{ get; set; }

      /// <summary>
/// 字节类型
/// </summary>
public byte[] ByteBinary { get; set; } /// <summary>
/// long类型
/// </summary>public long IconId { get; set; } /// <summary>
/// 字符串类型
/// </summary>public string Title { get; set; }
      /// <summary>
/// 时间类型
/// </summary>public DateTime PushTime { get; set; } /// <summary>
/// bool类型
/// </summary>public bool PushNow { get; set; }
      /// <summary>
/// long的集合类型
/// </summary>
List<long> TestLong { get; set; } = new List<long> { , , , , , }; /// <summary>
/// string的集合类型
/// </summary>
List<string> TestString { get; set; } = new List<string> {"","","" }; }
SendToWebByHttpClient("www.....",new SendData{
            FileList =要提交的数据,
            File=要提交的数据,
            ByteBinary=要提交的数据,
              .
              .
              .
              .
              .            })

请求帮助     

       /// <summary> 模拟表单请求/liuyl/2017/09/1 /// </summary>        
      /// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="value"></param>
/// <returns></returns>
public static ErrorCode SendToWebByHttpClient<T>(string url, T value)
{
var modelType = typeof(T);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
          //遍历SendData的所有成员
foreach (var item in modelType.GetProperties())
{
HttpContent content;
            //文件的处理(这里需要特别注意,流只能读取一次,因为读取之后会把Stream.Positon(当前流中的位置)置为最后读取的位置,除非置为0,第二次才可读到)
if (item.PropertyType == typeof(HttpPostedFileBase) && item.GetValue(value) != null)
{
#region Stream请求
//Stream塞进Content会会导致读取这个流,所以之后不能再第二次利用
var model = (HttpPostedFileBase)item.GetValue(value);
content = new StreamContent(model.InputStream, model.ContentLength);
               content.Headers.ContentType = MediaTypeHeaderValue.Parse(model.ContentType);//ContentType必须赋值,否则文件接收不到此属性
content.Headers.ContentLength = model.ContentLength;//ContentLength可不显式的赋值,会自动读取给StreamContent的内容长度
formData.Add(content, item.Name, model.FileName);//文件类型,第三个参数必须要赋值,否则不认为这是一个文件 #endregion #region 字节方式请求
//var model = (HttpPostedFileBase)item.GetValue(value);
//MemoryStream fileTarget = new MemoryStream();
//model.InputStream.CopyTo(fileTarget);
//content = new ByteArrayContent(fileTarget.ToArray());
//content.Headers.ContentType = MediaTypeHeaderValue.Parse(model.ContentType);
//content.Headers.ContentLength = model.ContentLength;
//formData.Add(content, item.Name, model.FileName); #endregion
}
//文件的处理
else if (item.PropertyType == typeof(HttpPostedFileWrapper) && item.GetValue(value) != null)
{
#region Stream请求
var model = (HttpPostedFileWrapper)item.GetValue(value);
content = new StreamContent(model.InputStream, model.ContentLength);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(model.ContentType);
content.Headers.ContentLength = model.ContentLength;
formData.Add(content, item.Name, model.FileName); #endregion
#region 字节方式请求
//var model = (HttpPostedFileWrapper)item.GetValue(value);
//MemoryStream fileTarget = new MemoryStream();
//model.InputStream.CopyTo(fileTarget);
//content = new ByteArrayContent(fileTarget.ToArray());
//content.Headers.ContentType = MediaTypeHeaderValue.Parse(model.ContentType);
//content.Headers.ContentLength = model.ContentLength;
//formData.Add(content, item.Name, model.FileName); #endregion }
//文件集合的处理
else if (item.PropertyType == typeof(List<HttpPostedFileBase>) && item.GetValue(value) != null)
{
foreach (var child in ((List<HttpPostedFileBase>)item.GetValue(value)))
{
#region Stream请求
content = new StreamContent(child.InputStream, child.ContentLength);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(child.ContentType);
content.Headers.ContentLength = child.ContentLength;
formData.Add(content, item.Name, child.FileName);
#endregion #region 字节方式请求
//MemoryStream fileTarget = new MemoryStream();
//child.InputStream.CopyTo(fileTarget);
//content = new ByteArrayContent(fileTarget.ToArray());
//content.Headers.ContentType = MediaTypeHeaderValue.Parse(child.ContentType);
//content.Headers.ContentLength = child.ContentLength;
//formData.Add(content, item.Name, child.FileName); #endregion
}
}
//如果执意响应方是接收字节类型,那传输时不能用ByteArrayContent去填充,否则接收方认为这是一个非法数据,故要传base64格式,接收方会自动把base64转成字节接收
else if (item.PropertyType == typeof(byte[]) && item.GetValue(value) != null)
{
content = new StringContent(Convert.ToBase64String((byte[])item.GetValue(value)));
formData.Add(content, item.Name);
}
//其他类型统一按字符串处理(DateTime,Enum;long ;bool;int...)
else if (item.GetValue(value) != null && (item.PropertyType != typeof(byte[]) || item.PropertyType != typeof(HttpPostedFileBase)))
{
content = new StringContent(((string)item.GetValue(value).ToString()));
formData.Add(content, item.Name);
}
} var response = client.PostAsync(url, formData).Result; if (!response.IsSuccessStatusCode)
{
            //以下根据自己业务处理返回值
var obj = JsonHandler.DeserializeObject<BaseViewModel>(response.ToString());
if (obj != null)
{
var result = obj.ErrResult;
if (result.ErrorCode != ErrorCode.OK)
{
foreach (var message in result.Messages)
{
_Error += message;
}
return result.ErrorCode;
}
}
}
return ErrorCode.OK;
}
}
partial class InternalController:Controller
{
/// <summary>
/// 接收方
/// </summary>
/// <param name="model"></param>
/// <returns>HTTP200</returns>
[HttpPost]
[ValidateInput(false)]//忽略安全检查,要配合在<system.web>中加入<httpRuntime requestValidationMode =“2.0”/>方可启用,详情参考:了解MVC的请求验证
public ActionResult AddOfficialNews(SendData model)
{
try
{
if (!ModelState.IsValid)
return SendJsonResult(ErrorCode.InvalidOperation);
              .....
        }
     }  
  }

需要特别注意(特别注意点在请求帮助类中已用黄底背景标注):

如果是上传的文件类型,一定不能在塞入StreamContent等之前进行过读取操作,否则此处将读不到流数据

参考:

https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload

通过C#的HttpClient模拟form表单请求的更多相关文章

  1. 模拟form表单请求上传文件

    发请求 public string CameraFileUpload(string url,string path,string serverPath,string uploadfileName) { ...

  2. nodejs 模拟form表单上传文件

    使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...

  3. js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题

    js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...

  4. js_ajax模拟form表单提交_多文件上传_支持单个删除

    需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...

  5. Linux curl 模拟form表单提交信息和文件

    Linux curl 模拟form表单提交信息和文件   curl是一个命令行方式下传输数据的开源传输工具,支持多种协议:FTP.HTTP.HTTPS.IMAP.POP3.TELNET等,功能超级强大 ...

  6. java如何区分是form表单请求,还是ajax请求

    requestType = request.getHeader("X-Requested-With");                 if(requestType==null) ...

  7. 使用HttpClient 传送form 表单的请求

    在项目中用到了,需要使用HttpClient 进行模拟表单传送form 表单的需求,在平常的项目中,大概都是传送json串的样式需求,但是如何才能给对应的服务器传送一个form 表单呢? 这就需要了N ...

  8. Ajax模拟Form表单提交,含多种数据上传

    ---恢复内容开始--- Ajax提交表单.使用FormData提交表单数据和上传的文件(这里的后台使用C#获取,你可以使用Java一样获取) 有时候前台的数据提交到后台,不想使用form表单上传,希 ...

  9. jquery模拟form表单提交并新打开页面

    /** * form表单提交本页面打开 * @param url * @param params */ function postCurrent(url,params){ var form = $(& ...

随机推荐

  1. Oracle EBS AR 冲销收款

    DECLARE L_CR_ID NUMBER; L_ATTRIBUTE_REC AR_RECEIPT_API_PUB.ATTRIBUTE_REC_TYPE; L_GLOBAL_ATT_REC AR_R ...

  2. iftop – 实时Linux网络带宽监控工具

    在本文中,我们提出了另一个称为Interface TOP (IFTOP)的优秀程序, 它是一个基于实时控制台的网络带宽监控工具. 它将显示接口上网络活动的快速概览. Iftop 平均每 2,10 和4 ...

  3. gitlab配置push -f 关闭

    默认路径是/var/opt/gitlab/git-data/repositories/组/库 修改conf 文件 [core] repositoryformatversion = 0 filemode ...

  4. Java-控制台传递参数

    今天组长叫我把所有的参数(写死的),用控制器输入,使其变成可变的. ------我的程序是需要读取文件的,控制台输入即,是文件放在哪我都可以读取. 比如我需要读取的demo.txt文件在D盘根目录下, ...

  5. Windows API串口编程详解

    (一)Windows API串口通信编程概述 Windows环境下的串口编程与DOS环境下的串口编程有很大不同.Windows环境下的编程的最大特征之一就是设备无关性,它通过设备驱动程序将Window ...

  6. priority_queue的优先级变化(结构体的写法)

    priority_queue的优先级变化(结构体的写法) 在头文件中加上#include <queue> 即可使用stl中的库函数priority_queue,优先队列默认的是从大到小的优 ...

  7. python第三十课--异常(else讲解)

    演示else语句和异常处理机制结合使用 try: print('try...') print(10/0) except: print('except...') else: print('else... ...

  8. c++ ActiveX基础1:使用VS2010创建MFC ActiveX工程项目

    1.ActiveX的基本概念 ActiveX控件可以看作是一个极小的服务器应用程序,它不能独立运行,必须嵌入到某个容器程序中,与该容器一起运行.这个容器包括WEB网页,应用程序窗体等... Activ ...

  9. 2018-2019-2 20165302 《网络对抗技术》Exp3 免杀原理与实践

    一,实验要求 (我是真的不知道为什么截图中都有学号和姓名信息了还要求改主机名......而且实验要求里也没有明确说.做完之后听别人说的,肯定是不能重新截图了,要扣分就扣吧) 通过本部分内容的学习,认识 ...

  10. RPC使用rabbitmq实现

    两天时间重写公司架构在本地实现测试学习 双向连接客户端和服务端配置: 连接rabbitmq服务器 定义消息队列 配置发送请求的模板:交换机.消息队列. 配置监听处理:监听的队列.消息转换处理 配置处理 ...