发送:使用httpclient
public static String postXML(String url,String xmlFileName){
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
log.info(xmlFileName);
try{
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
client = HttpClients.createDefault();
StringEntity entityParams = new StringEntity(xmlFileName,"utf-8");
httpPost.setEntity(entityParams);
client = HttpClients.createDefault();
resp = client.execute(httpPost);
String resultMsg = EntityUtils.toString(resp.getEntity(),"utf-8");
return resultMsg;
}catch (Exception e){
log.info(e.getMessage());
}finally {
try {
if(client!=null){
client.close();
}
if(resp != null){
resp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
使用request接收xml报文:
String line = null;
String xmlData ="";
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
while ((line = br.readLine()) != null) {
sb.append(line);
}
byte[] xmlDataByte = sb.toString().getBytes(StandardCharsets.UTF_8);
xmlData = new String(xmlDataByte, "UTF-8");
注意:
httpcliet使用 UrlEncodingFormEntity 封装报文 会进行转码