Webservice调用&测试
返回以xml字符串返回,xml某个字段是一个json字符串。请求头增加Content-type:text/xml。请求body以xml字符串传递。SOAPAction:方法名。
webservice调用方式:
(1)http方式调用
请求头增加Content-type:text/xml 或application/soap+xml
SOAPAction:方法名
请求body以xml字符串传递,xml格式定义
返回以xml字符串返回,xml某个字段是一个json字符串。
入参如下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dhcc="http://www.dhcc.com.cn">
<soapenv:Header/>
<soapenv:Body>
<dhcc:方法名>
<!--Optional:-->
<dhcc:inputJsonStream><![CDATA[
json串...
]]></dhcc:inputJsonStream>
</dhcc:方法名>
</soapenv:Body>
</soapenv:Envelope>
curl -XPOST -H 'Content-Type:text/xml' -d '<xml></xml>' webserviceurl
curl -X POST -H 'Content-Type:application/soap+xml' -d @test.xml webserviceurl,其中xml文件以本地文件的方式传递
public static String callWebService(String webserviceurl, String method, String sendMsg, String contentType) {
String retStr = "";
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(webserviceurl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", contentType);
httpPost.setHeader("SOAPAction", method);
StringEntity data = new StringEntity(sendMsg,
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.debug("url:{} ret:{}", url, retStr);
return retStr;
}
} catch (Exception e) {
logger.error("callWebService", e);
} finally {
// 释放资源
try {
closeableHttpClient.close();
} catch (IOException e) {
logger.error("close", e);
}
}
return null;
}
(2)通过apache的webservice工具调用
Client client = WsClientUtil.getClient(notifyUrl);
try {
Object[] objects = client.invoke(method, getjson());
if (objects != null) {
result = (String)objects[0];
log.info(result);
log.info("耗时:" +(System.currentTimeMillis() - start));
}
} catch (Exception e) {
throw new RuntimeException("WebSocket请求异常");
}
}
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 获取Client
*/
public class WsClientUtil {
private static final JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
private static final Map<String, Client> clientMap = new ConcurrentHashMap(256);
public static Client getClient(String wsUrl) {
// 创建动态客户端
// JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
//根据WebServices接口地址创建client
if (clientMap.get(wsUrl) == null) {
synchronized (WsClientUtil.class) {
if (clientMap.get(wsUrl) == null) {
Client client = clientFactory.createClient(wsUrl);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setAllowChunking(false);
// 连接服务器超时时间 3秒
policy.setConnectionTimeout(5000);
// 等待服务器响应超时时间 3秒
// 等待服务器响应超时时间 3秒
policy.setReceiveTimeout(5000);
conduit.setClient(policy);
clientMap.put(wsUrl, client);
return client;
}
}
}
return clientMap.get(wsUrl);
}
}
但今天测试时,使用curl -X POST -H 'Content-Type:application/soap+xml' -d '' 可以通,用java客户端调用就报错。。具体为什么还不知道。


调用的webservice地址需要带wsdl后缀,但加了后缀又报以下错误

15:43:15.010 [main] WARN org.apache.cxf.phase.PhaseInterceptorChain - Interceptor for {http://www.dhcc.com.cn}PUB0034#{http://www.dhcc.com.cn}YWQPatientSignCallBack has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:67)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:441)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:356)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:334)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
at org.szyx.ywx.es.transfer.historyorder.Main.main(Main.java:27)
Caused by: java.io.IOException: IOException invoking http://36.7.171.234/csp/hsb/DHC.Published.PUB0034.BS.PUB0034.cls: Error writing to server
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1417)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1401)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:688)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63)
... 8 common frames omitted
Caused by: java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:664)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:676)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1532)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream$2.run(URLConnectionHTTPConduit.java:377)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream$2.run(URLConnectionHTTPConduit.java:373)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream.getResponseCode(URLConnectionHTTPConduit.java:373)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1618)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1649)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1591)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1388)
... 11 common frames omitted
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:67)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:441)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:356)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:334)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
at org.szyx.ywx.es.transfer.historyorder.Main.main(Main.java:27)
Caused by: java.io.IOException: IOException invoking http://36.7.171.234/csp/hsb/DHC.Published.PUB0034.BS.PUB0034.cls: Error writing to server
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1417)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1401)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:688)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63)
... 8 more
Caused by: java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:664)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:676)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1532)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream$2.run(URLConnectionHTTPConduit.java:377)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream$2.run(URLConnectionHTTPConduit.java:373)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream.getResponseCode(URLConnectionHTTPConduit.java:373)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1618)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1649)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1591)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1388)
... 11 more
和我调用时传的参数是xml有关么?改成json字符串再试下,又变成了connection reset的错误了

最终找到代理端,修改wsdl文件缓存到代理服务器本地解决的。
wsdl远程文件有缓存?
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)