使用的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