最近对接了几个运营商的接口,整的也是焦头烂额的,好歹是完成了。
先记录一下这个,测试的话直接soapUI更方便,其他的后续整理整理再慢慢发布吧 。
public static void main(String args[]) throws Exception {
String url = "http://ip:port/url?wsdl";
StringBuilder soapBuilder = new StringBuilder(64);
soapBuilder.append("");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
.setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
.setSocketTimeout(3 * 1000) // 数据传输的最长时间10s
.build();
httpPost.setConfig(config);
CloseableHttpResponse response = null;
try {
//采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
// httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
StringEntity stringEntity = new StringEntity(soapBuilder.toString(), Charset.forName("UTF-8"));
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
// 200为成功
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//返回报文内容
System.out.println(content);
} else {
System.out.println("调用失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
}
}