cxf 和 httpclient 客户端调用 webservice 接口
一、cxf 生成 webservice 客户端
1、接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
2、进入你需要放置 webservice 客户端代码的包,进入这个包所在的系统路径,进入 cmd
3、执行命令 wsimport -keep http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 或者 wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
3.1、如果报错信息如下:具有相同名称“xxx”的类/接口已经使用。
wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
改为
wsdl2java -client -autoNameResolution http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
4、spring 整合 cxf
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="userClient" serviceClass="com.java.webservice.service.impl.ITianQi" <!--生成的接口--> address="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"> </jaxws:client> </beans>
二、httpclient 调用 webservice
public static String getXML() { StringBuffer sb = new StringBuffer(); sb.append("<?xml version='1.0' encoding='utf-8'?>" + "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + "<soap:Body>" + "<getMobileCodeInfo xmlns='http://WebXml.com.cn/'>" + "<mobileCode>string</mobileCode>" + " <userID>string</userID>" + "</getMobileCodeInfo>" + "</soap:Body>" + "</soap:Envelope>"); return sb.toString(); }
static int socketTimeout = 30000;// 请求超时时间 static int connectTimeout = 30000;// 传输超时时间 public static String doPost() { String postUrl = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"; String soapAction = ""; String soapXml = getXML(); 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", "application/soap+xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", soapAction); 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"); } // 释放资源 closeableHttpClient.close(); } catch (Exception e) { } return retStr; }