C#记一次http协议multipart/form-data的boundary问题

作者:地狱高速公路 时间:2021-07-01 17:45:49 

1.问题描述

使用post方法调用上级联网厂家接口,返回http状态码415,返回信息Content type ‘application/x-www-form-urlencoded’ not supported

测试上级联网厂家接口使用的是Postman工具,工具下载地址:https://www.getpostman.com/downloads/

使用application/x-www-form-urlencoded调用接口,返回http状态码415,如图:

C#记一次http协议multipart/form-data的boundary问题

既然服务器无法处理请求附带的媒体格式,那么改用multipart/form-data试试?

测试后发现可以调用成功,如图:

C#记一次http协议multipart/form-data的boundary问题

我们都知道ContentType为application/x-www-form-urlencoded的请求头、体如何构造,如:

HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
request.Host = Properties.Settings.Default.IP;
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.Accept = "*/*";
request.CookieContainer = cc;
request.KeepAlive = true;
string postData = string.Format("action=cx&hphm={0}&hpzl=&jclb=&detlsh=&clpp=&clxh=&rlzl=&pfbz=&jcff=&evl=&staName={1}&detLineId=&syxz=&rqyi={2}&rqer={3}&RQXZ=JCRQ&CXJL=Jiance&cllb=&clgs=&zcrq=&zzl=&clsbdh=&syr=&ccdjrq=&page={4}&rows=10", hphm, staName, rqyi, rqer, page);
byte[] postdatabyte = Encoding.GetEncoding("utf-8").GetBytes(postData);
request.ContentLength = postdatabyte.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postdatabyte, 0, postdatabyte.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
string temp = reader.ReadToEnd();
}

但是!multipart/form-data的请求头与请求体该如何构造呢?像下面这样?

string url = "http://112.17.158.12:8180/intf/services/query";
HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
request.Host = "112.17.158.12:8180";
request.ContentType = "multipart/form-data; ";
request.UserAgent = "PostmanRuntime/7.17.1";
request.Accept = "*/*";
request.KeepAlive = true;
string postData = @"jkuser=33088102&jkpasswd=33088102&jsondata={""jkid"":""R10"",""requestTime"":""20190919110603"",""body"":[{""inspstationcode"":""33088102""}]}";
byte[] postdatabyte = Encoding.GetEncoding("utf-8").GetBytes(postData);
request.ContentLength = postdatabyte.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postdatabyte, 0, postdatabyte.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
string temp = reader.ReadToEnd();
}

C#记一次http协议multipart/form-data的boundary问题

显然不对,捕获到了"远程服务器返回错误:(500)内部服务器错误。"异常,那么我们该怎么办呢?

2.解决思路

既然Postman工具使用ContentType为multipart/form-data类型post数据可以成功,那么我们写的C#程序应该也可以呀!那怎么办呢?。。。。没错!!就是抓包!

首先想到的是,用fiddler抓Postman的数据包,然后C#程序构造同样的数据包即可。

观察数据包Headers选项卡,如图:

C#记一次http协议multipart/form-data的boundary问题

发现Content-Type: multipart/form-data;后面跟了一个boundary=----------------------------183584948778966847113836

并且TextView选项卡如下:

C#记一次http协议multipart/form-data的boundary问题

WebForms选项卡如下:

C#记一次http协议multipart/form-data的boundary问题

所以,可以按照Headers和TextView选项卡内容构造post请求,就可以解决我们的问题了!

3.解决步骤

将ContentType加上boundary=boundary-------------------------xxxxxxxxxxxxxx

并且构造参数,如下:

string url = "http://112.17.158.12:8180/intf/services/query";
string boundary = "--------------------------" + DateTime.Now.Ticks.ToString("x");
string boundary2 = "--" + boundary;
HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
request.Host = "112.17.158.12:8180";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.UserAgent = "PostmanRuntime/7.17.1";
request.Accept = "*/*";
request.KeepAlive = true;
StringBuilder sb = new StringBuilder();
sb.Append(boundary2 + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""jkuser""" + "\r\n\r\n");
sb.Append("33088102" + "\r\n");
sb.Append(boundary2 + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""jkpasswd""" + "\r\n\r\n");
sb.Append("33088102" + "\r\n");
sb.Append(boundary2 + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""jsondata""" + "\r\n\r\n");
sb.Append(@"{""jkid"":""R10"",""requestTime"":""20190919110603"",""body"":[{""inspstationcode"":""33088102""}]}" + "\r\n");
sb.Append(boundary2 + "--" + "\r\n");
string postData = sb.ToString();
byte[] postdatabyte = Encoding.GetEncoding("utf-8").GetBytes(postData);
request.ContentLength = postdatabyte.Length;
using (Stream stream = request.GetRequestStream())
{
   stream.Write(postdatabyte, 0, postdatabyte.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
   string temp = reader.ReadToEnd();
}

值得注意的是,“boundary-------------------------xxxxxxxxxxxxxx”

这么长一串东西,只是作为分隔符出现的,不必太在意它是什么东西,我将它理解为分割文本参数的这么一个东西,并且通过仔细观察发现可以发现header中contenttype的横线数量比参数中横线数量少两个且必须少两个?

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

来源:https://cylycgs.blog.csdn.net/article/details/101019468

标签:C#,http,multipart,form-data,boundary
0
投稿

猜你喜欢

  • opencv实现轮廓高斯滤波平滑

    2023-07-23 02:05:56
  • Android App中ViewPager与Fragment结合的一些问题解决

    2023-08-02 22:22:43
  • C# 通过同步和异步实现优化做早餐的时间

    2022-10-27 23:09:27
  • MyBatis常用的jdbcType数据类型

    2023-09-18 19:09:35
  • SpringBoot整合Mybatis实现CRUD

    2022-01-10 09:26:45
  • java开发MVC三层架构上再加一层Manager层原理详解

    2023-06-14 06:10:51
  • java synchronized关键字的用法

    2022-11-19 13:45:25
  • Android编程实现等比例显示图片的方法

    2022-05-20 03:37:34
  • 公共POI导出Excel方法详解

    2023-09-10 22:51:31
  • c# base64转字符串实例

    2021-06-25 01:47:54
  • Spring容器中添加bean的5种方式

    2023-03-23 03:03:15
  • win7中C#的winForm编程使用savefiledialog不能弹出保存窗体的解决方法

    2022-09-18 07:27:57
  • Android自定义控件ListView下拉刷新的代码

    2023-04-07 23:51:06
  • Java函数式编程(六):Optional

    2021-07-04 21:44:05
  • Java并发之线程池Executor框架的深入理解

    2022-03-13 10:20:55
  • swagger添加权限验证保证API(接口)安全性(两种方法)

    2023-11-27 18:58:33
  • SpringBoot 在项目启动之后执行自定义方法的两种方式小结

    2021-05-25 15:46:36
  • springboot中validator数据校验功能的实现

    2021-07-31 17:43:50
  • C#使用iTextSharp封装的PDF文件操作类实例

    2023-05-16 22:26:52
  • C#直线的最小二乘法线性回归运算实例

    2022-05-03 11:19:33
  • asp之家 软件编程 m.aspxhome.com