org.apache.http.conn.ClientConnectionManager Java Examples

The following examples show how to use org.apache.http.conn.ClientConnectionManager. 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: HttpService.java    From oxAuth with MIT License 7 votes vote down vote up
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();
    }
}
 
Example #2
Source File: HttpClientFactory.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static HttpClient getTrustedClient(int httpPort, int httpsPort, File keystore, String keypass) {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), httpPort));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), httpsPort));

    HttpParams params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DispatchHttpClient(keystore, keypass);
}
 
Example #3
Source File: MySSLSocketFactory.java    From Mobike with Apache License 2.0 6 votes vote down vote up
/**
 * Gets getUrl DefaultHttpClient which trusts getUrl set of certificates specified by the KeyStore
 *
 * @param keyStore custom provided KeyStore instance
 * @return DefaultHttpClient
 */
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();
    }
}
 
Example #4
Source File: Util.java    From AppServiceRestFul with GNU General Public License v3.0 6 votes vote down vote up
private static HttpClient getNewHttpClient() { 
   try { 
       KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
       trustStore.load(null, null); 

       SSLSocketFactory sf = new SSLSocketFactoryEx(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); 

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

       ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

       return new DefaultHttpClient(ccm, params); 
   } catch (Exception e) { 
       return new DefaultHttpClient(); 
   } 
}
 
Example #5
Source File: MySSLSocketFactory.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore
 *
 * @param keyStore custom provided KeyStore instance
 * @return DefaultHttpClient
 */
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();
    }
}
 
Example #6
Source File: M3u8ContentParser.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
private DefaultHttpClient createHttpClient() {
	HttpParams params = new BasicHttpParams();
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(params,
			HTTP.DEFAULT_CONTENT_CHARSET);
	HttpProtocolParams.setUseExpectContinue(params, true);
	HttpConnectionParams.setConnectionTimeout(params,
			CONNETED_TIMEOUT * 1000);
	HttpConnectionParams.setSoTimeout(params, CONNETED_TIMEOUT * 1000);
	HttpConnectionParams.setSocketBufferSize(params, 8192);
	ConnManagerParams.setMaxTotalConnections(params, 4);
	SchemeRegistry schReg = new SchemeRegistry();
	schReg.register(new Scheme("http", PlainSocketFactory
			.getSocketFactory(), 80));
	schReg.register(new Scheme("https",
			SSLSocketFactory.getSocketFactory(), 443));

	ClientConnectionManager connMgr = new ThreadSafeClientConnManager(
			params, schReg);

	return new DefaultHttpClient(connMgr, params);
}
 
Example #7
Source File: RestProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void run() {
    try {
        while (!shutdown) {
            synchronized (this) {
                wait(1000);
                for (ClientConnectionManager connectionManager : connectionManagers) {
                    connectionManager.closeExpiredConnections();
                    // TODO constant
                    connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }
    } catch (InterruptedException ex) {
        shutdown();
    }
}
 
Example #8
Source File: RestProtocol.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public void run() {
    try {
        while (!shutdown) {
            synchronized (this) {
                wait(1000);
                for (ClientConnectionManager connectionManager : connectionManagers) {
                    connectionManager.closeExpiredConnections();
                    // TODO constant
                    connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }
    } catch (InterruptedException ex) {
        shutdown();
    }
}
 
Example #9
Source File: RestProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void run() {
    try {
        while (!shutdown) {
            synchronized (this) {
                wait(1000);
                for (ClientConnectionManager connectionManager : connectionManagers) {
                    connectionManager.closeExpiredConnections();
                    // TODO constant
                    connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }
    } catch (InterruptedException ex) {
        shutdown();
    }
}
 
Example #10
Source File: HttpClientFactory.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static HttpClient getClient(int httpPort, int httpsPort) {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(cm, params);

}
 
Example #11
Source File: MockHttpClient.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
protected RequestDirector createClientRequestDirector(
    HttpRequestExecutor requestExec,
    ClientConnectionManager conman,
    ConnectionReuseStrategy reustrat,
    ConnectionKeepAliveStrategy kastrat,
    HttpRoutePlanner rouplan,
    HttpProcessor httpProcessor,
    HttpRequestRetryHandler retryHandler,
    RedirectHandler redirectHandler,
    AuthenticationHandler targetAuthHandler,
    AuthenticationHandler proxyAuthHandler,
    UserTokenHandler stateHandler,
    HttpParams params) {
  return new RequestDirector() {
    @Beta
    public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
        throws HttpException, IOException {
      return new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, null);
    }
  };
}
 
Example #12
Source File: TestSSLWrong.java    From hk with GNU General Public License v3.0 6 votes vote down vote up
public HttpClient getNewHttpClient() {
   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);

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

       ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

       return new DefaultHttpClient(ccm, params);
   } catch (Exception e) {
       return new DefaultHttpClient();
   }

}
 
Example #13
Source File: RestProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void run() {
    try {
        while (!shutdown) {
            synchronized (this) {
                wait(1000);
                for (ClientConnectionManager connectionManager : connectionManagers) {
                    connectionManager.closeExpiredConnections();
                    // TODO constant
                    connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }
    } catch (InterruptedException ex) {
        shutdown();
    }
}
 
Example #14
Source File: EMQClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
public static HttpClient generateHttpClient(final int maxTotalConnections,
                                            final int maxTotalConnectionsPerRoute, int connTimeout) {
  HttpParams params = new BasicHttpParams();
  ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections);
  ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() {
    @Override
    public int getMaxForRoute(HttpRoute route) {
      return maxTotalConnectionsPerRoute;
    }
  });
  HttpConnectionParams
          .setConnectionTimeout(params, connTimeout);
  SchemeRegistry schemeRegistry = new SchemeRegistry();
  schemeRegistry.register(
          new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
  sslSocketFactory.setHostnameVerifier(SSLSocketFactory.
          ALLOW_ALL_HOSTNAME_VERIFIER);
  schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params,
          schemeRegistry);
  return new DefaultHttpClient(conMgr, params);
}
 
Example #15
Source File: MySSLSocketFactory.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
/**
 * Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore
 *
 * @param keyStore custom provided KeyStore instance
 * @return DefaultHttpClient
 */
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();
    }
}
 
Example #16
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 #17
Source File: WeixinUtil.java    From android-common-utils with Apache License 2.0 6 votes vote down vote up
private static HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new SSLSocketFactoryEx(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);

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

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}
 
Example #18
Source File: AbstractGoogleClientFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Replicates {@link ApacheHttpTransport#newDefaultHttpClient()} with one exception:
 *
 * 1 retry is allowed.
 *
 * @see DefaultHttpRequestRetryHandler
 */
DefaultHttpClient newDefaultHttpClient(
    SSLSocketFactory socketFactory, HttpParams params, ProxySelector proxySelector) {
  SchemeRegistry registry = new SchemeRegistry();
  registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  registry.register(new Scheme("https", socketFactory, 443));
  ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry);
  DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager, params);
  // retry only once
  defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true));
  if (proxySelector != null) {
    defaultHttpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector));
  }
  defaultHttpClient.setKeepAliveStrategy((response, context) -> KEEP_ALIVE_DURATION);
  return defaultHttpClient;
}
 
Example #19
Source File: NFHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Monitor(name = "HttpClient-MaxTotalConnections", type = DataSourceType.INFORMATIONAL)
public int getMaxTotalConnnections() {
	ClientConnectionManager connectionManager = this.getConnectionManager();
	if (connectionManager != null) {
		return ((ThreadSafeClientConnManager)connectionManager).getMaxTotal();
	} else {
		return 0;
	}
}
 
Example #20
Source File: AndroidHttpClient.java    From android-download-manager with Apache License 2.0 5 votes vote down vote up
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
	this.delegate = new DefaultHttpClient(ccm, params) {
		@Override
		protected BasicHttpProcessor createHttpProcessor() {
			// Add interceptor to prevent making requests from main thread.
			BasicHttpProcessor processor = super.createHttpProcessor();
			processor.addRequestInterceptor(sThreadCheckInterceptor);
			processor.addRequestInterceptor(new CurlLogger());

			return processor;
		}

		@Override
		protected HttpContext createHttpContext() {
			// Same as DefaultHttpClient.createHttpContext() minus the
			// cookie store.
			HttpContext context = new BasicHttpContext();
			context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
					getAuthSchemes());
			context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
					getCookieSpecs());
			context.setAttribute(ClientContext.CREDS_PROVIDER,
					getCredentialsProvider());
			return context;
		}
	};
}
 
Example #21
Source File: AndroidHttpClient.java    From android-download-manager with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

	// We use a factory method to modify superclass initialization
	// parameters without the funny call-a-static-method dance.
	return new AndroidHttpClient(manager, params);
}
 
Example #22
Source File: BuddycloudHTTPHelper.java    From buddycloud-android with Apache License 2.0 5 votes vote down vote up
public static HttpClient createSecureHttpClient() {
	try {
		SchemeRegistry registry = new SchemeRegistry();
		registry.register(new Scheme("https", createSecureSocketFactory(),
				443));
		ClientConnectionManager ccm = new SingleClientConnManager(
				new DefaultHttpClient().getParams(), registry);
		return new DefaultHttpClient(ccm, null);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #23
Source File: GooglePlayAPI.java    From raccoon4 with Apache License 2.0 5 votes vote down vote up
/**
 * Connection manager to allow concurrent connections.
 * 
 * @return {@link ClientConnectionManager} instance
 */
public static ClientConnectionManager getConnectionManager() {
	PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
			SchemeRegistryFactory.createDefault());
	connManager.setMaxTotal(100);
	connManager.setDefaultMaxPerRoute(30);
	return connManager;
}
 
Example #24
Source File: GooglePlayAPI.java    From dummydroid with Apache License 2.0 5 votes vote down vote up
/**
 * Connection manager to allow concurrent connections.
 *
 * @return {@link ClientConnectionManager} instance
 */
public static ClientConnectionManager getConnectionManager() {
	PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
			SchemeRegistryFactory.createDefault());
	connManager.setMaxTotal(100);
	connManager.setDefaultMaxPerRoute(30);
	return connManager;
}
 
Example #25
Source File: AndroidHttpClient.java    From travelguide with Apache License 2.0 5 votes vote down vote up
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
    this.delegate = new DefaultHttpClient(ccm, params) {
        @Override
        protected BasicHttpProcessor createHttpProcessor() {
            // Add interceptor to prevent making requests from main thread.
            BasicHttpProcessor processor = super.createHttpProcessor();
            processor.addRequestInterceptor(sThreadCheckInterceptor);
            processor.addRequestInterceptor(new CurlLogger());

            return processor;
        }

        @Override
        protected HttpContext createHttpContext() {
            // Same as DefaultHttpClient.createHttpContext() minus the
            // cookie store.
            HttpContext context = new BasicHttpContext();
            context.setAttribute(
                    ClientContext.AUTHSCHEME_REGISTRY,
                    getAuthSchemes());
            context.setAttribute(
                    ClientContext.COOKIESPEC_REGISTRY,
                    getCookieSpecs());
            context.setAttribute(
                    ClientContext.CREDS_PROVIDER,
                    getCredentialsProvider());
            return context;
        }
    };
}
 
Example #26
Source File: DispatchHttpClient.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(
            new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", newSslSocketFactory(), 443));
    return new SingleClientConnManager(getParams(), registry);
}
 
Example #27
Source File: AndroidHttpClient.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
    this.delegate = new DefaultHttpClient(ccm, params) {
        @Override
        protected BasicHttpProcessor createHttpProcessor() {
            // Add interceptor to prevent making requests from main thread.
            BasicHttpProcessor processor = super.createHttpProcessor();
            processor.addRequestInterceptor(sThreadCheckInterceptor);
            processor.addRequestInterceptor(new CurlLogger());

            return processor;
        }

        @Override
        protected HttpContext createHttpContext() {
            // Same as DefaultHttpClient.createHttpContext() minus the
            // cookie store.
            HttpContext context = new BasicHttpContext();
            context.setAttribute(
                    ClientContext.AUTHSCHEME_REGISTRY,
                    getAuthSchemes());
            context.setAttribute(
                    ClientContext.COOKIESPEC_REGISTRY,
                    getCookieSpecs());
            context.setAttribute(
                    ClientContext.CREDS_PROVIDER,
                    getCredentialsProvider());
            return context;
        }
    };
}
 
Example #28
Source File: BaseClientFactory.java    From galaxy-sdk-java with Apache License 2.0 5 votes vote down vote up
private static HttpClient generateHttpClient() {
  HttpParams params = new BasicHttpParams();
  ConnManagerParams.setMaxTotalConnections(params, 1);
  HttpConnectionParams
      .setConnectionTimeout(params, (int) CommonConstants.DEFAULT_CLIENT_CONN_TIMEOUT);
  SchemeRegistry schemeRegistry = new SchemeRegistry();
  schemeRegistry.register(
      new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
  sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
  return new DefaultHttpClient(conMgr, params);
}
 
Example #29
Source File: ApacheHttpTransport.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of the Apache HTTP client that is used by the {@link
 * #ApacheHttpTransport()} constructor.
 *
 * @param socketFactory SSL socket factory
 * @param params HTTP parameters
 * @param proxySelector HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code
 *     null} for {@link DefaultHttpRoutePlanner}
 * @return new instance of the Apache HTTP client
 */
static DefaultHttpClient newDefaultHttpClient(
    SSLSocketFactory socketFactory, HttpParams params, ProxySelector proxySelector) {
  // See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
  SchemeRegistry registry = new SchemeRegistry();
  registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  registry.register(new Scheme("https", socketFactory, 443));
  ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry);
  DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager, params);
  defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
  if (proxySelector != null) {
    defaultHttpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector));
  }
  return defaultHttpClient;
}
 
Example #30
Source File: EasyHttpClient.java    From mobilecloud-15 with Apache License 2.0 5 votes vote down vote up
/**
 * Function that creates a ClientConnectionManager which can handle http and https. 
 * In case of https self signed or invalid certificates will be accepted.
 */
@Override
protected ClientConnectionManager createClientConnectionManager() {		
	HttpParams params = new BasicHttpParams();
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(params, "utf-8");
	params.setBooleanParameter("http.protocol.expect-continue", false);
	
	SchemeRegistry registry = new SchemeRegistry();
	registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT));
	registry.register(new Scheme("https", new EasySSLSocketFactory(), HTTPS_PORT));
	ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
				
	return manager;
}