用SOAPUI 生成报文! 然后 用httpclient 代替cxf等webservice 框架 实现webservice 接口调用!!
package com.sinosoft.test;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class TestPro {
public static void main(String[] args) {
String url = "";
String param = "";
test(param, url);
}
public static void test(String param, String url){
System.out.println("请求报文:" + param);
try {
String result = doPost2webserivce(url, buiderPostParam(param), "");
System.out.println(result);
if(result != null) {
result = getResponseBody(result);
System.out.println("请求处理报文:" + result);
} else {
System.out.println("没有收到响应数据");
}
} catch (Exception e) {
e.printStackTrace();
};
}
public static String buiderPostParam(String param) {
StringBuilder paramBuilder = new StringBuilder();
paramBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.wspre.sinosoft.com\">");
paramBuilder.append("<soapenv:Header/>");
paramBuilder.append("<soapenv:Body>");
paramBuilder.append("<web:service>");
paramBuilder.append("<![CDATA[");
paramBuilder.append(param);
paramBuilder.append("]]>");
paramBuilder.append("</web:service>");
paramBuilder.append("</soapenv:Body>");
paramBuilder.append("</soapenv:Envelope>");
return paramBuilder.toString();
}
private static String doPost2webserivce(String webServiceUrl, String param, String soapAction) throws Exception {
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//设置请求超时
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(600000).setConnectTimeout(600000)
.setSocketTimeout(600000).build();
HttpPost httpost = new HttpPost(webServiceUrl);
httpost.setConfig(requestConfig);
httpost.setHeader("Content-Type", "text/xml;charset=GBK");
httpost.setHeader("SOAPAction", soapAction);
StringEntity stringEntity = new StringEntity(param,"GBK");
httpost.setEntity(stringEntity);
CloseableHttpResponse response = httpclient.execute(httpost);
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
}
response.close();
} finally {
httpclient.close();
}
return result;
}
private static String getResponseBody(String result) throws Exception {
Document document = DocumentHelper.parseText(result);
Element rootElement = document.getRootElement();
Element returnElment = rootElement.element("Body").element("responseInfo");
return returnElment.asXML();
}
}