使用的httpclient,傳參需要傳xml,剛開始調用返回正常,但是客戶系統上顯式的是亂碼,然後查閱資料,經過領導點播最終解決。
剛開始使用的soap UI測試將請求頭修改爲text/xml;charset=GBK測試成功,也不是亂碼,客戶系統正常
然後修改代碼:
將自己的header設置爲:
header.put("Content-type", "text/xml;charset=GBK");
/**
* 提交安全問題
*
* @return
*/
public static String SubmitAQWT(String xml) {
String url = "http://10.128.30.50/ssmiswebservice/ssmiswebservice.asmx";
Map<String, String> header = new HashMap<String, String>();
header.put("Content-type", "text/xml;charset=GBK");
String result = "";
try {
LOG.info("================開始向路局同步數據=======================");
result = doPost(url, header, xml);
LOG.info("結果:", result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
發現還是亂碼,百思不得其解,然後又改了請求體 編碼 StringEntity entity = new StringEntity(body, "GBK");然後成功了。雖然成功了但是我還是有點傷心,那麼簡單的問題搞了那麼久才搞出來,感覺對不起自己的生命。
public static String doPost(String url, Map<String, String> header, String body) throws Exception {
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
//設置header
Iterator<Map.Entry<String, String>> iteratorHeader = header.entrySet().iterator();
while (iteratorHeader.hasNext()) {
Map.Entry<String, String> elem = iteratorHeader.next();
httpPost.setHeader(elem.getKey(), elem.getValue());
}
//設置body
if (body != null) {
StringEntity entity = new StringEntity(body, "GBK");
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "UTF-8");
}
}
return result;
}
作爲一個初級程序員發現自己的水平還是不夠啊,好傷心。有時間補補編碼的知識+httpclient