Java calls other people's soap interfaces We
often use third-party interfaces in our projects. Today we will talk about two ways to call other people's soap-style interfaces in java. The first method
:
the first method is the commonly used feign. The focus of the above picture
is to annotate @FeignClient and @Headers. After getting the string returned in the interface, it can be parsed at the business layer.
The second way:
The second way is to use httpClient to submit a post request,
just parse the above figure and return the string!
The second method appends the code as follows:
private String getResult(String url, String xml) throws Exception {
PostMethod postMethod = new PostMethod(url);
// 然后把Soap请求数据添加到PostMethod中
byte[] b = xml.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml; charset=UTF-8");
postMethod.setRequestEntity(re);
// 最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
String soapResponseData= "";
if(statusCode == 200) {
log.info("调用成功!");
soapResponseData = postMethod.getResponseBodyAsString();
}
else {
log.info("调用失败!错误码:" + statusCode);
}
return soapResponseData;
}
It's over, sprinkle flowers~~~~