<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
使用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)內部伺服器錯誤。"異常,那麼我們該怎麼辦呢?
既然Postman工具使用ContentType為multipart/form-data型別post資料可以成功,那麼我們寫的C#程式應該也可以呀!那怎麼辦呢?。。。。沒錯!!就是抓包!
首先想到的是,用fiddler抓Postman的封包,然後C#程式構造同樣的封包即可。
觀察封包Headers索引標籤,如圖:
發現Content-Type: multipart/form-data;後面跟了一個boundary=----------------------------183584948778966847113836
並且TextView索引標籤如下:
WebForms索引標籤如下:
所以,可以按照Headers和TextView索引標籤內容構造post請求,就可以解決我們的問題了!
將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 + "rn"); sb.Append(@"Content-Disposition: form-data; name=""jkuser""" + "rnrn"); sb.Append("33088102" + "rn"); sb.Append(boundary2 + "rn"); sb.Append(@"Content-Disposition: form-data; name=""jkpasswd""" + "rnrn"); sb.Append("33088102" + "rn"); sb.Append(boundary2 + "rn"); sb.Append(@"Content-Disposition: form-data; name=""jsondata""" + "rnrn"); sb.Append(@"{""jkid"":""R10"",""requestTime"":""20190919110603"",""body"":[{""inspstationcode"":""33088102""}]}" + "rn"); sb.Append(boundary2 + "--" + "rn"); 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的橫線數量比引數中橫線數量少兩個且必須少兩個?
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45