亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

http post請求,java和net的問題

http post請求,java和net的問題

白豬掌柜的 2018-12-06 23:42:22
我用fiddler和C#寫的一個請求XML var client = new HttpClient(); var content = new StringContent(strXML, Encoding.GetEncoding("GBK")); //content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml"); var result = client.PostAsync(apiUrl, content).Result; return result.Content.ReadAsStringAsync().Result; 都報異常,返回的異常信息 <?xml version="1.0" encoding="gb2312"?><PACKET><HEAD><SystemCode></SystemCode><ResponseType>0</ResponseType><ErrorMessage><![CDATA[The element type "CarOwner" must be terminated by the matching end-tag "</CarOwner>".]]></ErrorMessage></HEAD></PACKET> ?接口供應商給我說的意思是我這里對流長度做限制了 改用java寫是正常的 public static String net(String strUrl, String method) throws Exception { HttpURLConnection conn = null; BufferedReader reader = null; String rs = null; try { URL url = new URL(strUrl); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setRequestProperty("Pragma:", "no-cache"); con.setRequestProperty("Cache-Control", "no-cache"); con.setRequestProperty("Content-Type", "text/xml"); OutputStreamWriter out = new OutputStreamWriter(con .getOutputStream()); String xmlInfo=readFileContent(FILEXML); System.out.println("urlStr=" + strUrl); System.out.println("xmlInfo=" +xmlInfo); out.write(xmlInfo); out.flush(); out.close(); BufferedReader br = new BufferedReader(new InputStreamReader(con .getInputStream())); String line = ""; for (line = br.readLine(); line != null; line = br.readLine()) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); } } return rs; } 大神誰知道是怎么回事,困擾一星期了
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

問題分析:

? ? ? ? ?用C#出錯,從錯誤消息中:The element type "CarOwner" must be terminated by the matching end-tag "</CarOwner>". 說明在讀取xml時出錯,認為你的xml沒有結束標記</CarOwner>

? ? ? ? ? 用Java正常,說明xml文件是沒有問題的。

? ? ? ? ? 只能是在讀取XML時采取的字符集不對,導致讀取XML是沒有讀完。并且還可以說明你的XML中肯定是有中文,并且讀取結束的地方,就在讀取中文后沒多遠。

修改方案:

? ? ? ?Encoding.GetEncoding("GBK") ??改用UTF-8

查看完整回答
反對 回復 2018-12-16
?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

有中文么,查看編碼格式是否正確,你用了GBK,注應該在頭里面寫清楚,在你的text/xml換成text/xml;charset=utf-8試一試。

至于說到長度的問題,應該不會出現問題的,因為httpclient會處理好的,它會把你寫入數據的長度算出來,然后放到http頭塊里面,這個http協議的實現,你不需要管。你試一下是不是中英文的問題,或者假如你有特殊字符也會出現被中斷的問題。

查看完整回答
反對 回復 2018-12-16
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

1、斷點檢查你的XML是不是真的閉合了;

2、你的ContentType注釋掉干嘛?或者你可以直接看StringContent構造函數的第三個是什么,直接設置成text/xml不得了。

然后再試試

查看完整回答
反對 回復 2018-12-16
  • 3 回答
  • 0 關注
  • 707 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號