今天调试WebService服务的时候遇到一个问题,WebService的服务端是用Jaxws(JDK自带)构建,测试用的客户端是用Eclipse自动生成。
在每次客户端调用的时候,服务端能接受请求,并处理请求,但是返回时候,客户端不能立即得到响应,而需要大约5分钟/300000g毫秒才能结果。
代码类似:
服务端代码com.je.ws.HelloService.java:
package com.je.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(name="helloService" ,portName="helloServicePort" ,targetNamespace="http://www.javaedu.com") public class HelloService { @WebMethod public String hello(String name){ System.out.println("Sev.Hello:" + name); return "OK, " + name.length; } public static void main(String[] args){ Endpoint.publish("http://192.168.3.120:8080/HelloServicePort", new HelloService()); } }
客户端代码,省去由Eclipse自动生成代码,客户端调用代码:com.je.test.JestClient.java:
package com.je.test; import java.rmi.RemoteException; public class JestClient { public static void main(String[] args) throws RemoteException { HelloServiceProxy jp = new HelloServiceProxy(); String hello = jp.hello("xidada"); System.out.println(hello); } }
问同事没人见过,Google/百度也少有答案。最后不断更换条件,终于找到答案了。原因竟然是Http协议的版本问题…… 在eclipse 生成的方法中,XXXPortBindingStub.java调用方法中添加
_call.setProperty(MessageContext.HTTP_TRANSPORT_VERSION,HTTPConstants.HEADER_PROTOCOL_V11);
添加之后,OK。更深层原因不明,等待高手答疑,希望对大家有帮助。