webservice调用
导入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
编写httpclient工具类
package com.ghrc.demo.user;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class HttpClientCallSoapUtil {
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static Logger logger = Logger.getLogger(HttpClientCallSoapUtil.class);
/**
* 使用SOAP1.1发送消息
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public String doPostSoap1_1(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
//设置请求头部信息
httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
// httpPost.setHeader("X-Powered-By", "ASP.NET");
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doPostSoap1_1", e);
}
return retStr;
}
}
编写测试类
@Test
public void sad() throws Exception {
String orderSoapXml = "<soap:Envelope xmlns:xsi=\"http://xxxxxxx\" xmlns:xsd=\"http://xxxx\" xmlns:soap=\"http://schemas.xmlsoap.xxxxxxx/\">\n" +
" <soap:Body>\n" +
" <GetAllUserList xmlns=\"http://xxxxx.org/\" />\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
String postUrl = "http://xxxxxxxxx/xxxxxxx.asmx?op=GetAllUserList";
HttpClientCallSoapUtil callSoapUtil = new HttpClientCallSoapUtil();
String s = callSoapUtil.doPostSoap1_1(postUrl, orderSoapXml, "");
System.out.println(s);
这些信息是从那个接口获得的,推荐用这个工具访问给你暴露的地址 工具soapui
把用工具得到的信息放在orderSoapXml里面
String s 就是得到的就是接口返回的xml格式的内容
使用dom4j解析这个xml格式的字符串
dom4j的依赖已经导入maven里面了
根据xml格式的解析就好了
SAXReader saxReader = new SAXReader();
Document read = saxReader.read(new ByteArrayInputStream(s.getBytes("UTF-8")));
//获取根节点
Element rootElement = read.getRootElement();
// 获取子节点
Element body = rootElement.element("Body");
Element getAllUserListResponse = body.element("GetAllUserListResponse");
Element userList = getAllUserListResponse.element("GetAllUserListResult");
Element userEntity = userList.element("UserEntity");
List<Element> elements = userList.elements();
for (int i = 0; i < elements.size(); i++) {
Element element = elements.get(i);
String userid = element.elementText("USERID");
String loginname = element.elementText("LOGINNAME");
String username = element.elementText("USERNAME");
String functional = element.elementText("FUNCTIONAL");
String company = element.elementText("COMPANY");
String departmentid = element.elementText("DEPARTMENTID");
String title = element.elementText("TITLE");
String department = element.elementText("DEPARTMENT");
}
之后就把xml里的信息根据属性值就把这个取出来了
完工!!!