Android DefaultHttpClient默认超时

时间:2013-09-04 13:28:18

标签: android http

我的问题是如果我没有指定它,使用DefaultHttpClient发出的请求的默认超时是什么。

所以,如果没有像这样的代码

HttpParams my_httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
HttpConnectionParams.setSoTimeout(my_httpParams, 1);

但只是

HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(params,
                HTTP.DEFAULT_CONTENT_CHARSET);
ClientConnectionManager cm = new ThreadSafeClientConnManager(params,
                schemeRegistry);
SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
return new DefaultHttpClient(cm, params);

此httpClient等待服务器响应多长时间?

2 个答案:

答案 0 :(得分:3)

据我所知,默认情况下DefaultHttpClient的连接超时和套接字超时都为空(或零),这意味着不使用超时,Android应用程序将在理论上等待连接和套接字响应完成。因此,强烈建议在使用DefaultHttpClient时提供新的连接和套接字超时。

答案 1 :(得分:0)

我在源代码中做了一些窥探并发现了这两种方法。所以看起来他们默认为0。

/**
 * Obtains value of the {@link CoreConnectionPNames#CONNECTION_TIMEOUT}
 * parameter. If not set, defaults to <code>0</code>.
 *
 * @param params HTTP parameters.
 * @return connect timeout.
 */
public static int getConnectionTimeout(final HttpParams params) {
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    return params.getIntParameter
        (CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
}

/**
 * Obtains value of the {@link CoreConnectionPNames#SO_TIMEOUT} parameter.
 * If not set, defaults to <code>0</code>.
 *
 * @param params HTTP parameters.
 * @return SO_TIMEOUT.
 */
public static int getSoTimeout(final HttpParams params) {
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    return params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0);
}