简体   繁体   English

HttpClient - 设置“全局”套接字超时,以及每个请求单独的超时

[英]HttpClient - setting a “global” socket timeout, and a separate timeout per request

With HttpClient, I am setting the default socket/connection timeout with the following: 使用HttpClient,我使用以下内容设置默认套接字/连接超时:

HttpParams params = new BasicHttpParams();

HttpConnectionParams.setSoTimeout(params, 30000);
HttpConnectionParams.setConnectionTimeout(params, 30000);

mClient = new DefaultHttpClient(connectionManager, params);

I'm wondering if I can override these values on a per request basis? 我想知道我是否可以在每个请求的基础上覆盖这些值?

Edit: Would this work? 编辑:这会有用吗?

HttpParams params = req.getParams(); // req is an HttpRequest object
HttpConnectionParams.setSoTimeout(params, 60000);
HttpConnectionParams.setConnectionTimeout(params, 60000);

I tried it, and it seems to, but it's hard to test/create a situation where a timeout will occur. 我试过了,但似乎很难测试/创建一个超时发生的情况。

If you are using HttpClient 4.0 you could do this : 如果您使用的是HttpClient 4.0,您可以这样做:

mClient = new DefaultHttpClient(connectionManager, params) {
  protected HttpParams determineParams(HttpRequest req) {
    //Fill in your impl here
 }

You can simply set those parameters on the request object. 您只需在请求对象上设置这些参数即可。 For details see: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e391 详情请见: http//hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e391

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM