本文介绍了Java : HttpClient 4.1.2 : ConnectionTimeout、SocketTimeout 值设置无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 HttpClient 4.1.2.将 ConnectionTimeout 和 SocketTimeout 设置为一个值永远不会有效.

代码:

 Long startTime = null;DefaultHttpClient httpClient = new DefaultHttpClient();HttpParams params = httpClient.getParams();HttpConnectionParams.setConnectionTimeout(params, 30);HttpConnectionParams.setSoTimeout(params, 60);HttpGet httpget = new HttpGet("http://localhost:8080/Test/ScteServer");尝试 {startTime = System.currentTimeMillis();HttpResponse 响应 = httpClient.execute(httpget);}捕获(SocketTimeoutException se){Long endTime = System.currentTimeMillis();System.out.println("SocketTimeoutException :: time elapsed :: " + (endTime-startTime));se.printStackTrace();}捕获(连接超时异常 cte){Long endTime = System.currentTimeMillis();System.out.println("ConnectTimeoutException :: time elapsed :: " + (endTime-startTime));cte.printStackTrace();}捕获(ClientProtocolException e){e.printStackTrace();}捕获(IOException e){Long endTime = System.currentTimeMillis();System.out.println("IOException :: time elapsed :: " + (endTime-startTime) );e.printStackTrace();}

如果服务器关闭,那么连接超时永远不会超过 400 毫秒,因为它必须按照配置在 ~ 30 毫秒内超时.

套接字超时的情况也是如此,在 doGet() 中休眠 5000 毫秒将引发套接字超时,该超时永远不会达到配置的 60 毫秒左右.耗时超过 500 毫秒.

谁能建议如何配置 HttpClient 4.1.2 使其在配置的时间内超时?

解决方案

HttpConnectionParams 需要传递给连接管理器(请参阅 这个问题).使用 DefaultHttpClient 时,您可以像这样设置这些参数:

 httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

另请参阅文档!>

I am using HttpClient 4.1.2. Setting ConnectionTimeout and SocketTimeout to a value is never effective.

code :

  Long startTime = null;
  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpParams params = httpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 30);
  HttpConnectionParams.setSoTimeout(params, 60);
   HttpGet httpget = new HttpGet("http://localhost:8080/Test/ScteServer");
      try {
        startTime = System.currentTimeMillis();
        HttpResponse response = httpClient.execute(httpget);
      }
      catch(SocketTimeoutException se) {
        Long endTime = System.currentTimeMillis();
        System.out.println("SocketTimeoutException :: time elapsed :: " + (endTime-startTime));
        se.printStackTrace();
      }
      catch(ConnectTimeoutException cte) {
        Long endTime = System.currentTimeMillis();
        System.out.println("ConnectTimeoutException :: time elapsed :: " + (endTime-startTime));
        cte.printStackTrace();
      }
      catch (ClientProtocolException e) {
        e.printStackTrace();
      }
      catch (IOException e) {
        Long endTime = System.currentTimeMillis();
        System.out.println("IOException :: time elapsed :: " + (endTime-startTime) );
        e.printStackTrace();
      }

If the server is down, then the connection timeout is never before 400 ms when it has to timeout at ~ 30 ms as configured.

Same is the case for Socket Timeout, putting a sleep in doGet() for 5000 ms will throw a socket timeout which will never be at around 60 ms as configured. It takes more than 500 ms.

Can anyone suggest how to configure HttpClient 4.1.2 so that it times out around the configured time?

解决方案

The HttpConnectionParams need to be passed to a connection manager (see this question). When using the DefaultHttpClient you can set these parameters like this:

    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

See the documentation also!

这篇关于Java : HttpClient 4.1.2 : ConnectionTimeout、SocketTimeout 值设置无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:41