/** * @see SchemeRegistryFactory#createDefault() */ private MonitoredConnectionManager createDefaultSslCM() { final SchemeRegistry registry = new SchemeRegistry(); registry.register( new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register( new Scheme("https", 443, new SSLSocketFactoryAdapter(SSLConnectionSocketFactory.getSocketFactory()))); return new MonitoredConnectionManager(clientName, registry); } }
sock = createSocket(); int timeout = HttpConnectionParams.getConnectionTimeout(params);
protected HttpClient createHttpClient() { DefaultHttpClient client = new DefaultHttpClient(createClientConnectionManager()); if (useCompression) { client.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { // We expect to received a compression response that we un-gzip request.addHeader("Accept-Encoding", "gzip"); } }); } if (getProxyHost() != null) { HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (client.getConnectionManager().getSchemeRegistry().get("http") == null) { client.getConnectionManager().getSchemeRegistry().register( new Scheme("http", getProxyPort(), PlainSocketFactory.getSocketFactory())); } if(getProxyUser() != null && getProxyPassword() != null) { client.getCredentialsProvider().setCredentials( new AuthScope(getProxyHost(), getProxyPort()), new UsernamePasswordCredentials(getProxyUser(), getProxyPassword())); } } HttpParams params = client.getParams(); HttpConnectionParams.setSoTimeout(params, soTimeout); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); return client; }
ssf = PlainSocketFactory.getSocketFactory(); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", Integer.parseInt(port), ssf)); PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry); httpClient = new DefaultHttpClient(ccm); httpHost = new HttpHost(hosts[0], Integer.parseInt(port), "http"); ((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() ((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor()
BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) {
SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme(targetServer.getProtocol(), getPortOrDefault(targetServer.getPort()), PlainSocketFactory .getSocketFactory())); BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpParams, config.getSocketTimeout()); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setDefaultMaxPerRoute(config.getMaxConnections()); cm.setMaxTotal(config.getMaxConnections()); DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams); httpClient.removeResponseInterceptorByClass(ResponseProcessCookies.class); httpClient.removeRequestInterceptorByClass(RequestAddCookies.class);
public static synchronized HttpClient getInstance() throws MalformedURLException { if (instance == null) { final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setStaleCheckingEnabled(params, false); ConnManagerParams.setMaxTotalConnections(params, 1000); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1000)); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry .register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5984)); schemeRegistry .register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ClientConnectionManager cm = new ShieldedClientConnManager( new ThreadSafeClientConnManager(params, schemeRegistry)); instance = new DefaultHttpClient(cm, params); instance.setCredentialsProvider(credsProvider); instance.addRequestInterceptor(new PreemptiveAuthenticationRequestInterceptor(), 0);
public static DefaultHttpClient buildDefaultSearchHttpClient(boolean ignoreSslIssues) { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", new PlainSocketFactory(), 80)); registry.register(new Scheme("https", ignoreSslIssues ? new IgnoreTlsSniSocketFactory() : new TlsSniSocketFactory(), 443)); HttpParams httpparams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpparams, 8000); HttpConnectionParams.setSoTimeout(httpparams, 8000); DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpparams, registry), httpparams); httpclient.addRequestInterceptor(HttpHelper.gzipRequestInterceptor); httpclient.addResponseInterceptor(HttpHelper.gzipResponseInterceptor); return httpclient; }
public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException { this.configuration = configuration; HttpProtocolParams.setContentCharset(globalParams, getConfiguration().getContentCharset()); HttpProtocolParams.setUseExpectContinue(globalParams, false); // These are some safety settings, we should never run into these timeouts as we // do our own expiration checking HttpConnectionParams.setConnectionTimeout(globalParams, (getConfiguration().getTimeoutSeconds()+5) * 1000); HttpConnectionParams.setSoTimeout(globalParams, (getConfiguration().getTimeoutSeconds()+5) * 1000); HttpConnectionParams.setStaleCheckingEnabled(globalParams, getConfiguration().getStaleCheckingEnabled()); if (getConfiguration().getSocketBufferSize() != -1) HttpConnectionParams.setSocketBufferSize(globalParams, getConfiguration().getSocketBufferSize()); // Only register 80, not 443 and SSL SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); clientConnectionManager = new PoolingClientConnectionManager(registry); clientConnectionManager.setMaxTotal(getConfiguration().getMaxTotalConnections()); clientConnectionManager.setDefaultMaxPerRoute(getConfiguration().getMaxTotalPerRoute()); httpClient = new DefaultHttpClient(clientConnectionManager, globalParams); if (getConfiguration().getRequestRetryCount() != -1) { httpClient.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler(getConfiguration().getRequestRetryCount(), false) ); } }
sslSf = new SSLSocketFactory(sslTs, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", 8080, PlainSocketFactory.getSocketFactory())); schemeRegistry.register( new Scheme("https", 443, sslSf)); schemeRegistry.register( new Scheme("https", 80, sslSf)); httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMs); HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMs); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setStaleCheckingEnabled(httpParams, true); HttpConnectionParams.setSoKeepalive(httpParams, true);
HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, connTimeout); // 设置连接超时时间 HttpConnectionParams.setSoTimeout(params, readTimeout); // 设置读取数据超时时间 SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); schemeRegistry.register(new Scheme("https", 443, sslSocketFactory)); final X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier(); if (!(delegate instanceof SSLVerifier)) { sslSocketFactory.setHostnameVerifier(new SSLVerifier(delegate));
protected HttpClient constructClient() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // use the debug proxy to view internet traffic on the host computer if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http")); // ignore ssl certification (due to signed authority not appearing on android list of permitted authorities) // see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/ SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", new PlainSocketFactory(), 80)); registry.register(new Scheme("https", new FakeSocketFactory(), 443)); ClientConnectionManager cm = new SingleClientConnManager(params, registry); return new DefaultHttpClient(cm, params); }
final HttpParams httpClientParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true); HttpConnectionParams.setTcpNoDelay(httpClientParams, true); final Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT); final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); final Scheme https = new Scheme("https", sslSocketFactory, HTTPS_PORT); final SchemeRegistry sr = connectionManager.getSchemeRegistry(); sr.register(http); sr.register(https);
public void setPortForScheme(String scheme, int port) { Scheme _scheme = new Scheme(scheme, PlainSocketFactory.getSocketFactory(), port); httpClient.getConnectionManager().getSchemeRegistry().register(_scheme); }
//In class XMLRPCClient public XMLRPCClient(URI uri) { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", new PlainSocketFactory(), 80)); registry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); postMethod = new HttpPost(uri); postMethod.addHeader("Content-Type", "text/xml"); // WARNING // I had to disable "Expect: 100-Continue" header since I had // two second delay between sending http POST request and POST body httpParams = postMethod.getParams(); HttpProtocolParams.setUseExpectContinue(httpParams, false); this .client = new DefaultHttpClient( new ThreadSafeClientConnManager(httpParams, registry), httpParams); }
/** * @since 4.1 */ public Socket connectSocket( final Socket socket, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpParams params) throws IOException, ConnectTimeoutException { Args.notNull(remoteAddress, "Remote address"); Args.notNull(params, "HTTP parameters"); Socket sock = socket; if (sock == null) { sock = createSocket(); } if (localAddress != null) { sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); sock.bind(localAddress); } final int connTimeout = HttpConnectionParams.getConnectionTimeout(params); final int soTimeout = HttpConnectionParams.getSoTimeout(params); try { sock.setSoTimeout(soTimeout); sock.connect(remoteAddress, connTimeout); } catch (final SocketTimeoutException ex) { throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); } return sock; }
/** * @return SocketFactory used by the underlying HttpClient */ protected SocketFactory getSocketFactory() { return PlainSocketFactory.getSocketFactory(); }
@Override public Socket createSocket(HttpParams params) { // See if our test parameter is present Object o = params.getParameter("testtesttest"); assert(o != null); return super.createSocket(params); }
/** * Gets the default factory. * * @return the default factory */ public static PlainSocketFactory getSocketFactory() { return new PlainSocketFactory(); }
protected HttpClient createHttpClient() { DefaultHttpClient client = new DefaultHttpClient(createClientConnectionManager()); if (useCompression) { client.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { // We expect to received a compression response that we un-gzip request.addHeader("Accept-Encoding", "gzip"); } }); } if (getProxyHost() != null) { HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (client.getConnectionManager().getSchemeRegistry().get("http") == null) { client.getConnectionManager().getSchemeRegistry().register( new Scheme("http", getProxyPort(), PlainSocketFactory.getSocketFactory())); } if(getProxyUser() != null && getProxyPassword() != null) { client.getCredentialsProvider().setCredentials( new AuthScope(getProxyHost(), getProxyPort()), new UsernamePasswordCredentials(getProxyUser(), getProxyPassword())); } } HttpParams params = client.getParams(); HttpConnectionParams.setSoTimeout(params, soTimeout); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); return client; }