Search in sources :

Example 21 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager 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 22 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project android-app by eoecn.

the class CustomHttpClient method getHttpClient.

/**
 * 创建httpClient实例
 *
 * @return
 * @throws Exception
 */
private static synchronized HttpClient getHttpClient(Context context) {
    if (null == customerHttpClient) {
        HttpParams params = new BasicHttpParams();
        // 设置一些基本参数
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, CHARSET_UTF8);
        HttpProtocolParams.setUseExpectContinue(params, true);
        HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) " + "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
        // 超时设置
        /* 从连接池中取连接的超时时间 */
        ConnManagerParams.setTimeout(params, 1000);
        /* 连接超时 */
        int ConnectionTimeOut = 3000;
        if (!HttpUtils.isWifiDataEnable(context)) {
            ConnectionTimeOut = 10000;
        }
        HttpConnectionParams.setConnectionTimeout(params, ConnectionTimeOut);
        /* 请求超时 */
        HttpConnectionParams.setSoTimeout(params, 4000);
        // 设置我们的HttpClient支持HTTP和HTTPS两种模式
        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        // 使用线程安全的连接管理来创建HttpClient
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
        customerHttpClient = new DefaultHttpClient(conMgr, params);
    }
    return customerHttpClient;
}
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 23 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project Libraries-for-Android-Developers by eoecn.

the class MySSLSocketFactory method getNewHttpClient.

/**
 * Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore
 *
 * @param keyStore
 * @return
 */
public static DefaultHttpClient getNewHttpClient(KeyStore keyStore) {
    try {
        SSLSocketFactory sf = new MySSLSocketFactory(keyStore);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception 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) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 24 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project oxAuth by GluuFederation.

the class HttpService method getHttpsClientTrustAll.

public HttpClient getHttpsClientTrustAll() {
    try {
        SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {

            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }, new AllowAllHostnameVerifier());
        PlainSocketFactory psf = PlainSocketFactory.getSocketFactory();
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, psf));
        registry.register(new Scheme("https", 443, sf));
        ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
        return new DefaultHttpClient(ccm);
    } catch (Exception ex) {
        log.error("Failed to create TrustAll https client", ex);
        return new DefaultHttpClient();
    }
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) CertificateException(java.security.cert.CertificateException) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) X509Certificate(java.security.cert.X509Certificate) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) SslDefaultHttpClient(org.gluu.net.SslDefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 25 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class ArtistTopTagsServiceTest method getArtistTopTagsClient.

@SuppressWarnings("unchecked")
private ArtistTopTagsClient getArtistTopTagsClient(WebserviceHistoryService historyService) throws IOException {
    // create a HTTP client that always returns Cher top tracks
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(CHER_TOP_TAGS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    // create a client based on mocked HTTP client
    ArtistTopTagsClient attClient = new ArtistTopTagsClient();
    attClient.setWebserviceHistoryService(historyService);
    attClient.setHttpClient(httpClient);
    attClient.setThrottleService(throttleService);
    return attClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) HttpClient(org.apache.http.client.HttpClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) ArtistTopTagsClient(com.github.hakko.musiccabinet.ws.lastfm.ArtistTopTagsClient)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)74 Scheme (org.apache.http.conn.scheme.Scheme)51 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)48 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)39 HttpParams (org.apache.http.params.HttpParams)37 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)35 BasicHttpParams (org.apache.http.params.BasicHttpParams)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)26 IOException (java.io.IOException)21 Test (org.junit.Test)15 CertificateException (java.security.cert.CertificateException)14 HttpClient (org.apache.http.client.HttpClient)14 KeyManagementException (java.security.KeyManagementException)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 SSLContext (javax.net.ssl.SSLContext)12 HttpResponse (org.apache.http.HttpResponse)11 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)11 KeyStoreException (java.security.KeyStoreException)10 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)9