Search in sources :

Example 16 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project LiveSDK-for-Android by liveservices.

the class LiveConnectClient method getHttpClient.

private static HttpClient getHttpClient() {
    // The LiveConnectClients can share one HttpClient with a ThreadSafeConnManager.
    if (HTTP_CLIENT == null) {
        synchronized (HTTP_CLIENT_LOCK) {
            if (HTTP_CLIENT == null) {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT_IN_MS);
                HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT_IN_MS);
                ConnManagerParams.setMaxTotalConnections(params, 100);
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
                // Create an HttpClient with the ThreadSafeClientConnManager.
                // This connection manager must be used if more than one thread will
                // be using the HttpClient, which is a common scenario.
                ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
                HTTP_CLIENT = new DefaultHttpClient(cm, params);
            }
        }
    }
    return HTTP_CLIENT;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 17 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project java-chassis by ServiceComb.

the class HttpsClient method getHttpsClient.

public static HttpClient getHttpsClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), PORT_80));
        registry.register(new Scheme("https", sf, PORT_443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (RuntimeException e) {
        LOGGER.error("Get https client runtime exception: {}", FortifyUtils.getErrorInfo(e));
        return new DefaultHttpClient();
    } catch (Exception e) {
        LOGGER.error("Get https client exception: {}", FortifyUtils.getErrorInfo(e));
        return new DefaultHttpClient();
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams) KeyStore(java.security.KeyStore) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 18 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project tdi-studio-se by Talend.

the class paloconnection method initConnection.

private void initConnection() {
    pingPaloServer();
    paloTargetHost = new HttpHost(strServer, Integer.valueOf(strPort), "http");
    SchemeRegistry supportedSchemes = new SchemeRegistry();
    supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), Integer.valueOf(strPort)));
    // prepare parameters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, true);
    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, supportedSchemes);
    paloHttpClient = new DefaultHttpClient(connMgr, params);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) HttpHost(org.apache.http.HttpHost) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 19 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project platform_external_apache-http by android.

the class AndroidHttpClient method newInstance.

/**
     * Create a new HttpClient with reasonable defaults (which you can update).
     *
     * @param userAgent to report in your HTTP requests
     * @param context to use for caching SSL sessions (may be null for no caching)
     * @return AndroidHttpClient for you to use for all your requests.
     */
public static AndroidHttpClient newInstance(String userAgent, Context context) {
    HttpParams params = new BasicHttpParams();
    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);
    // Use a session cache for SSL sockets
    SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(SOCKET_OPERATION_TIMEOUT, sessionCache), 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSessionCache(android.net.SSLSessionCache) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 20 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project voldemort by voldemort.

the class HttpClientBench method createClient.

private static HttpClient createClient() {
    ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), DEFAULT_CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    HttpParams clientParams = httpClient.getParams();
    HttpConnectionParams.setSocketBufferSize(clientParams, 60000);
    HttpConnectionParams.setTcpNoDelay(clientParams, false);
    HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT);
    HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1);
    // HostConfiguration hostConfig = new HostConfiguration();
    // hostConfig.setHost("localhost");
    HttpConnectionParams.setConnectionTimeout(clientParams, DEFAULT_CONNECTION_MANAGER_TIMEOUT);
    HttpConnectionParams.setSoTimeout(clientParams, 500);
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES);
    connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_HOST_CONNECTIONS);
    HttpConnectionParams.setStaleCheckingEnabled(clientParams, false);
    return httpClient;
}
Also used : HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)36 Scheme (org.apache.http.conn.scheme.Scheme)29 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)28 BasicHttpParams (org.apache.http.params.BasicHttpParams)28 HttpParams (org.apache.http.params.HttpParams)27 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)26 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)23 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)10 IOException (java.io.IOException)7 ConnPerRouteBean (org.apache.http.conn.params.ConnPerRouteBean)7 KeyManagementException (java.security.KeyManagementException)5 KeyStoreException (java.security.KeyStoreException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 UnrecoverableKeyException (java.security.UnrecoverableKeyException)5 CertificateException (java.security.cert.CertificateException)5 HttpHost (org.apache.http.HttpHost)5 UnknownHostException (java.net.UnknownHostException)4 SSLSessionCache (android.net.SSLSessionCache)3 KeyStore (java.security.KeyStore)3 GeneralSecurityException (java.security.GeneralSecurityException)2