基于http的网络连接中超时设置如下:
URL url = new URL("http://www.163.com");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setReadTimeout(5000);//设置从主机读取数据超时(单位:毫秒)
httpConnection.setConnectTimeout(20000); //设置连接主机超时(单位:毫秒)
httpConnection.connect();
在xfire生成webservice客户端设置超时:
//创建Service对象
MyServiceClient MyClient = new MyServiceClient();
MyService MyService = MyClient.getMyServicePort();
//设置连接参数
HttpClientParams params = new HttpClientParams();
params.setParameter(HttpClientParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
params.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, setting.getTimeout() * 1000);//单位是毫秒
Client client = Client.getInstance(MyService);
client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params);
XFire是基于Apache的HttpClient,所以实际上连接超时的参数是就是设置在HttpClient上的。
经过测试,使用HttpClient设置连接超时并没有起作用;(原因还有待查证)
不过可以直接使用HttpURLConnection 来设置