首先需要引入依赖jar包

#版本只供参考,具体看项目
<dependency>
<grouId>org.apache.cxf</grouId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<grouId>org.apache.cxf</grouId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>


JaxWsDynamicClientFactory

 简介:只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,不关心服务端的实现方式。


public class Client3 {  

public static void main(String[] args) throws Exception {
//方法名
String method = "sayHello"
//参数
String param = "KEVIN"
//实例化JaxWsDynamicClientFactory
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
//通过wsdl路径生成客户端实体
Client client = clientFactory.createClient("http://localhost:9090/helloWorldService?wsdl");
//调用方法传参
Object[] result = client.invoke(method, param);
//打印回值
System.out.println(result[0]);
}

}


webservie报错:远程主机强迫关闭了一个现有的连接。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection:远程 主机强迫关闭了一个现有的连接。. ---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at EnterpriseServerBase.WebService.DynamicWebCalling.HdyyElectronicService.doDownloadRecipeInfo(String userName, String password, String senderCode)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at XmlTrans.Core.WSHelperCore.InvokeWebServiceMethod(String methodname, Object[] args)

原因:

经验证,如果数据稍微大点,比如3-4M,有时候可以通过稍微调大等待超时时长来解决问题,但是如果数据偏大的话,比如10M,调大等待超时时长也无济于事,Socket连接很快会被干掉,只要数据一直这么大,连接就会被不断的干掉。

总结:

webservice只适用于小数据的传输,不要尝试使用webservice进行大数据的传输。

解决办法:

public class Client3 {  

public static void main(String[] args) throws Exception {
//方法名
String method = "sayHello"
//参数
String param = "KEVIN"
//实例化JaxWsDynamicClientFactory
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
//通过wsdl路径生成客户端实体
Client client = clientFactory.createClient("http://localhost:9090/helloWorldService?wsdl");

HTTPConduit http = (HTTPConduit)client.getConduit();
//策略
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//连接超时(1分钟)
httpClientPolicy.setConnectionTimeout(1 * 60000);
httpClientPolicy.setAllowChunking(false);
//响应超时(1分钟)
httpClientPolicy.setReceiveTimeout(1 * 60000);
http.setClient(httpClientPolicy);
//调用方法传参
Object[] result = client.invoke(method, param);
//打印回值
System.out.println(result[0]);
}

}

作者:怒吼的萝卜