網站首頁 編程語言 正文
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,如圖:
既然服務器無法處理請求附帶的媒體格式,那么改用multipart/form-data試試?
測試后發現可以調用成功,如圖:
我們都知道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();
}
顯然不對,捕獲到了"遠程服務器返回錯誤:(500)內部服務器錯誤。"異常,那么我們該怎么辦呢?
2.解決思路
既然Postman工具使用ContentType為multipart/form-data類型post數據可以成功,那么我們寫的C#程序應該也可以呀!那怎么辦呢?。。。。沒錯!!就是抓包!
首先想到的是,用fiddler抓Postman的數據包,然后C#程序構造同樣的數據包即可。
觀察數據包Headers選項卡,如圖:
發現Content-Type: multipart/form-data;后面跟了一個boundary=----------------------------183584948778966847113836
并且TextView選項卡如下:
WebForms選項卡如下:
所以,可以按照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
相關推薦
- 2022-07-12 windows版wsl docker desktop 安裝nginx
- 2022-04-30 DataGridView控件常用屬性介紹_C#教程
- 2023-01-13 碼云(gitee)通過git自動同步到阿里云服務器_服務器其它
- 2022-05-10 在 VSCode 中如何設置默認的瀏覽器為Chrome或Firefox
- 2022-08-20 Go?多環境下配置管理方案(多種方案)_Golang
- 2023-04-18 C++超詳細分析優化排序算法之堆排序_C 語言
- 2022-08-16 Python中集合的創建及常用函數的使用詳解_python
- 2022-12-13 torch.optim優化算法理解之optim.Adam()解讀_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支