Java Code Examples for org.apache.http.conn.params.ConnManagerParams#setTimeout()

The following examples show how to use org.apache.http.conn.params.ConnManagerParams#setTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpEngine.java    From letv with Apache License 2.0 6 votes vote down vote up
private HttpParams createHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, false);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(3));
    ConnManagerParams.setMaxTotalConnections(params, 3);
    ConnManagerParams.setTimeout(params, 1000);
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpClientParams.setRedirecting(params, false);
    return params;
}
 
Example 2
Source File: PoolingClientConnectionManager.java    From letv with Apache License 2.0 6 votes vote down vote up
public static HttpClient get() {
    HttpParams httpParams = new BasicHttpParams();
    ConnManagerParams.setTimeout(httpParams, 3000);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(30));
    ConnManagerParams.setMaxTotalConnections(httpParams, 30);
    HttpClientParams.setRedirecting(httpParams, true);
    HttpProtocolParams.setUseExpectContinue(httpParams, true);
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
    HttpConnectionParams.setSoTimeout(httpParams, 2000);
    HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTP_TAG, PlainSocketFactory.getSocketFactory(), 80));
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTPS_TAG, sf, 443));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
}
 
Example 3
Source File: MyHtttpClient.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
public synchronized static DefaultHttpClient getHttpClient() {
	try {
		HttpParams params = new BasicHttpParams();
		// 设置一些基本参数
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		// 超时设置
		// 从连接池中取连接的超时时间
		ConnManagerParams.setTimeout(params, 10000); // 连接超时
		HttpConnectionParams.setConnectionTimeout(params, 10000); // 请求超时
		HttpConnectionParams.setSoTimeout(params, 30000);
		SchemeRegistry registry = new SchemeRegistry();
		Scheme sch1 = new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80);
		registry.register(sch1);
		// 使用线程安全的连接管理来创建HttpClient
		ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
				params, registry);
		mHttpClient = new DefaultHttpClient(conMgr, params);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return mHttpClient;
}
 
Example 4
Source File: AsyncHttpClient.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public AsyncHttpClient(SchemeRegistry schemeregistry)
{
    a = 10;
    b = 10000;
    h = true;
    BasicHttpParams basichttpparams = new BasicHttpParams();
    ConnManagerParams.setTimeout(basichttpparams, b);
    ConnManagerParams.setMaxConnectionsPerRoute(basichttpparams, new ConnPerRouteBean(a));
    ConnManagerParams.setMaxTotalConnections(basichttpparams, 10);
    HttpConnectionParams.setSoTimeout(basichttpparams, b);
    HttpConnectionParams.setConnectionTimeout(basichttpparams, b);
    HttpConnectionParams.setTcpNoDelay(basichttpparams, true);
    HttpConnectionParams.setSocketBufferSize(basichttpparams, 8192);
    HttpProtocolParams.setVersion(basichttpparams, HttpVersion.HTTP_1_1);
    ThreadSafeClientConnManager threadsafeclientconnmanager = new ThreadSafeClientConnManager(basichttpparams, schemeregistry);
    e = getDefaultThreadPool();
    f = new WeakHashMap();
    g = new HashMap();
    d = new SyncBasicHttpContext(new BasicHttpContext());
    c = new DefaultHttpClient(threadsafeclientconnmanager, basichttpparams);
    c.addRequestInterceptor(new a(this));
    c.addResponseInterceptor(new b(this));
    c.addRequestInterceptor(new c(this), 0);
    c.setHttpRequestRetryHandler(new z(5, 1500));
}
 
Example 5
Source File: HTTPClientManager.java    From ForgePE with GNU Affero General Public License v3.0 6 votes vote down vote up
private HTTPClientManager() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");
    ConnManagerParams.setTimeout(params, 30000);
    params.setBooleanParameter("http.protocol.expect-continue", false);

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    SSLSocketFactory sslSocketFactory;
    try {
        registry.register(new Scheme("https", NoCertSSLSocketFactory.createDefault(), 443));
    } catch (Exception e) {
        Log.e("MCPE_ssl", "Couldn\\'t create SSLSocketFactory");
    }

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, registry);
    this.mHTTPClient = new DefaultHttpClient(manager, params);
}
 
Example 6
Source File: HttpClientFactory.java    From miappstore with Apache License 2.0 6 votes vote down vote up
private static HttpParams createHttpParams() {
	final HttpParams params = new BasicHttpParams();
	// 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。
	// 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间
	HttpConnectionParams.setStaleCheckingEnabled(params, false);
	HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间
	HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间
	HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小
	HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟)
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本
	HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制
	HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码
	HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向

	ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时
	ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数
	ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数
	return params;
}
 
Example 7
Source File: HttpClientFactory.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
private static HttpParams createHttpParams() {
	final HttpParams params = new BasicHttpParams();
	// 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。
	// 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间
	HttpConnectionParams.setStaleCheckingEnabled(params, false);
	HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间
	HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间
	HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小
	HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟)
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本
	HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制
	HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码
	HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向

	ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时
	ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数
	ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数
	return params;
}
 
Example 8
Source File: HttpUtils.java    From LitePlayer with Apache License 2.0 5 votes vote down vote up
public static HttpParams getParams() {
	HttpParams params = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(params, 4000);
	HttpConnectionParams.setSoTimeout(params, 10000);
	ConnManagerParams.setTimeout(params, 4000);
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
	HttpProtocolParams.setUseExpectContinue(params, true);
	HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 (Windows NT 6.1; WOW64)" +
			" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36");
	return params;
}
 
Example 9
Source File: AsyncHttpClient.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public void setTimeout(int i)
{
    if (i < 1000)
    {
        i = 10000;
    }
    b = i;
    HttpParams httpparams = c.getParams();
    ConnManagerParams.setTimeout(httpparams, b);
    HttpConnectionParams.setSoTimeout(httpparams, b);
    HttpConnectionParams.setConnectionTimeout(httpparams, b);
}
 
Example 10
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Set connection timeout limit (milliseconds). By default, this is set to
 * 10 seconds.
 *
 * @param value Connection timeout in milliseconds, minimal value is 1000 (1 second).
 */
public void setConnectTimeout(int value) {
    connectTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value;
    final HttpParams httpParams = httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, connectTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
}
 
Example 11
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Set connection timeout limit (milliseconds). By default, this is set to
 * 10 seconds.
 *
 * @param value Connection timeout in milliseconds, minimal value is 1000 (1 second).
 */
public void setConnectTimeout(int value) {
    connectTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value;
    final HttpParams httpParams = httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, connectTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
}
 
Example 12
Source File: FinalHttp.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 设置网络连接超时时间,默认为10秒钟
 * @param timeout
 */
public void configTimeout(int timeout){
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
}
 
Example 13
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Set connection timeout limit (milliseconds). By default, this is set to
 * 10 seconds.
 *
 * @param value Connection timeout in milliseconds, minimal value is 1000 (1 second).
 */
public void setConnectTimeout(int value) {
    connectTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value;
    final HttpParams httpParams = httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, connectTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
}
 
Example 14
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Set the connection and socket timeout. By default, 10 seconds.
 *
 * @param timeout the connect/socket timeout in milliseconds, at least 1 second
 */
public void setTimeout(int timeout) {
    if (timeout < 1000)
        timeout = DEFAULT_SOCKET_TIMEOUT;
    this.timeout = timeout;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, this.timeout);
    HttpConnectionParams.setSoTimeout(httpParams, this.timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
}
 
Example 15
Source File: ApacheHttpRequest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
  HttpParams params = request.getParams();
  ConnManagerParams.setTimeout(params, connectTimeout);
  HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
  HttpConnectionParams.setSoTimeout(params, readTimeout);
}
 
Example 16
Source File: HttpUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public HttpUtils configTimeout(int timeout) {
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
    return this;
}
 
Example 17
Source File: AsyncHttpClient.java    From Libraries-for-Android-Developers with MIT License 5 votes vote down vote up
/**
 * Set the connection and socket timeout. By default, 10 seconds.
 *
 * @param timeout the connect/socket timeout in milliseconds, at least 1 second
 */
public void setTimeout(int timeout) {
    if (timeout < 1000)
        timeout = DEFAULT_SOCKET_TIMEOUT;
    this.timeout = timeout;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, this.timeout);
    HttpConnectionParams.setSoTimeout(httpParams, this.timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
}
 
Example 18
Source File: ApacheClient.java    From android-lite-http with Apache License 2.0 5 votes vote down vote up
/**
 * initialize HttpParams , initialize settings such as total connextions,timeout ...
 */
private BasicHttpParams createHttpParams() {
    BasicHttpParams params = new BasicHttpParams();
    ConnManagerParams.setTimeout(params, DEFAULT_TIMEOUT);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(DEFAULT_MAX_CONN_PER_ROUT));
    ConnManagerParams.setMaxTotalConnections(params, DEFAULT_MAX_CONN_TOTAL);
    HttpConnectionParams.setTcpNoDelay(params, TCP_NO_DELAY);
    HttpConnectionParams.setConnectionTimeout(params, DEFAULT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, DEFAULT_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, DEFAULT_BUFFER_SIZE);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, userAgent);
    // settingOthers(params);
    return params;
}
 
Example 19
Source File: ThreadPoolHttpClient.java    From albert with MIT License 4 votes vote down vote up
public void test() throws Exception {
    exe = Executors.newFixedThreadPool(POOL_SIZE);
    HttpParams params =new BasicHttpParams();
    /* 从连接池中取连接的超时时间 */ 
    ConnManagerParams.setTimeout(params, 1000);
    /* 连接超时 */ 
    HttpConnectionParams.setConnectionTimeout(params, 2000); 
    /* 请求超时 */
    HttpConnectionParams.setSoTimeout(params, 4000);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(
            new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    //ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    PoolingClientConnectionManager cm=new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(10);
    final HttpClient httpClient = new DefaultHttpClient(cm,params);

    // URIs to perform GETs on
    final String[] urisToGet = urls;
    /* 有多少url创建多少线程,url多时机子撑不住
    // create a thread for each URI
    GetThread[] threads = new GetThread[urisToGet.length];
    for (int i = 0; i < threads.length; i++) {
        HttpGet httpget = new HttpGet(urisToGet[i]);
        threads[i] = new GetThread(httpClient, httpget);            
    }
    // start the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }

    // join the threads,等待所有请求完成
    for (int j = 0; j < threads.length; j++) {
        threads[j].join();
    }
    使用线程池*/
    for (int i = 0; i < urisToGet.length; i++) {
        final int j=i;
        System.out.println(j);
        HttpGet httpget = new HttpGet(urisToGet[i]);
        exe.execute( new GetThread(httpClient, httpget));
    }
    
    
    //创建线程池,每次调用POOL_SIZE
    /*
    for (int i = 0; i < urisToGet.length; i++) {
        final int j=i;
        System.out.println(j);
        exe.execute(new Thread() {
            @Override
            public void run() {
                this.setName("threadsPoolClient"+j);
                
                    try {
                        this.sleep(100);
                        System.out.println(j);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    HttpGet httpget = new HttpGet(urisToGet[j]);
                    new GetThread(httpClient, httpget).get();
                }
                
                
            
        });
    }
    
    */
    //exe.shutdown();
    System.out.println("Done");
}
 
Example 20
Source File: AsyncHttpClient.java    From letv with Apache License 2.0 4 votes vote down vote up
public void setTimeout(int timeout) {
    HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setTimeout(httpParams, (long) timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
}