开发者

httpclient timeouts? can's seem to get them to stick

Here is the code I'm using to create my multithreaded httpclient object. I'm trying to have a 4 second timeout across the board so if nothing happens for 4 seconds to drop the requests. I'm seeing really long execution times on some requests like in the 300 second range. I can't seem to figure out why that's happening. Any ideas?

HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 4000);
    HttpConnectionParams.setSoTimeout(httpParams, 4000);

    // set request params

    httpParams.setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
    httpParams.setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
    httpParams.setParameter("http.protocol.wait-for-continue", 4000);

    SchemeRegistry schemeRegistry = new Sc开发者_如何学运维hemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(httpParams,schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(cm, httpParams);


All the timeout values you set only try to limit the time for a specific operation, not total time of the request. In certain circumstances, the timeout doesn't work at all. For example, the connection timeout has no effect if the IP is blocked by firewall.

The only solution I found is to start my request in a different thread and time it out myself. You can use ThreadPool and FutureTask for this. See my answer to this question,

java native Process timeout

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜