List of usage examples for org.apache.http.conn.params ConnPerRoute ConnPerRoute
ConnPerRoute
From source file:com.thistech.spotlink.util.HttpClientFactory.java
public HttpClient newInstance() { HttpClient client = new DefaultHttpClient(); HttpParams httpParams = client.getParams(); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); if (this.properties.containsKey("httpclient.timeout")) { int timeout = Integer.parseInt(this.properties.getProperty("httpclient.timeout")); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); }// w ww . j a v a 2 s. c o m ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return Integer.parseInt(properties.getProperty("httpclient.conn-per-route", "5")); } }); int totalConnections = Integer.parseInt(this.properties.getProperty("httpclient.total-connections", "100")); ConnManagerParams.setMaxTotalConnections(httpParams, totalConnections); String userAgent = this.properties.getProperty("httpclient.user-agent", "Mozilla/5.0"); HttpProtocolParams.setUserAgent(httpParams, userAgent); String charset = this.properties.getProperty("httpclient.content-charset", "UTF-8"); HttpProtocolParams.setContentCharset(httpParams, charset); ClientConnectionManager mgr = client.getConnectionManager(); SchemeRegistry schemeRegistry = mgr.getSchemeRegistry(); client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams); return client; }
From source file:simple.crawler.http.HttpClientFactory.java
public static DefaultHttpClient createNewDefaultHttpClient() { ////from w w w . j a v a 2 s. c o m HttpParams params = new BasicHttpParams(); //Determines the connection timeout HttpConnectionParams.setConnectionTimeout(params, 1 * 60 * 1000); //Determines the socket timeout HttpConnectionParams.setSoTimeout(params, 1 * 60 * 1000); //Determines whether stale connection check is to be used HttpConnectionParams.setStaleCheckingEnabled(params, false); //The Nagle's algorithm tries to conserve bandwidth by minimizing the number of segments that are sent. //When application wish to decrease network latency and increase performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY) //Data will be sent earlier, at the cost of an increase in bandwidth consumption HttpConnectionParams.setTcpNoDelay(params, true); // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4"); //Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager pm = new PoolingClientConnectionManager(schemeRegistry); // DefaultHttpClient httpclient = new DefaultHttpClient(pm, params); //ConnManagerParams.setMaxTotalConnections(params, MAX_HTTP_CONNECTION); //ConnManagerParams.setMaxConnectionsPerRoute(params, defaultConnPerRoute); //ConnManagerParams.setTimeout(params, 1 * 60 * 1000); httpclient.getParams().setParameter("http.conn-manager.max-total", MAX_HTTP_CONNECTION); ConnPerRoute defaultConnPerRoute = new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 4; } }; httpclient.getParams().setParameter("http.conn-manager.max-per-route", defaultConnPerRoute); httpclient.getParams().setParameter("http.conn-manager.timeout", 1 * 60 * 1000L); httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true); httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); // HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount > 2) { return false; } if (exception instanceof NoHttpResponseException) { return true; } if (exception instanceof SSLHandshakeException) { return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; httpclient.setHttpRequestRetryHandler(retryHandler); HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip, deflate"); } } }; HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header header = entity.getContentEncoding(); if (header != null) { HeaderElement[] codecs = header.getElements(); for (int i = 0; i < codecs.length; i++) { String codecName = codecs[i].getName(); if ("gzip".equalsIgnoreCase(codecName)) { response.setEntity(new GzipDecompressingEntity(entity)); return; } else if ("deflate".equalsIgnoreCase(codecName)) { response.setEntity(new DeflateDecompressingEntity(entity)); return; } } } } }; httpclient.addRequestInterceptor(requestInterceptor); httpclient.addResponseInterceptor(responseInterceptor); httpclient.setRedirectStrategy(new DefaultRedirectStrategy()); return httpclient; }
From source file:se.kodapan.io.http.HttpAccessor.java
public synchronized void open() throws IOException { if (open) {// www .j av a 2s .c om return; } if (temporaryContentFilePath == null) { temporaryContentFilePath = File.createTempFile(HttpAccessor.class.getSimpleName(), "contentFiles"); temporaryContentFilePath.delete(); } if (!temporaryContentFilePath.exists()) { if (!temporaryContentFilePath.mkdirs()) { throw new IOException("Could not create directory: " + temporaryContentFilePath.getAbsolutePath()); } } else if (!temporaryContentFilePath.isDirectory()) { throw new IOException("Not a directory: " + temporaryContentFilePath.getAbsolutePath()); } HttpParams params = new BasicHttpParams(); params.setIntParameter("http.socket.timeout", 10000); // 10 seconds params.setIntParameter("http.connection.timeout", 10000); // 10 seconds /* 'http.conn-manager.timeout': defines the timeout in milliseconds used when retrieving an instance of ManagedClientConnection from the ClientConnectionManager This parameter expects a value of type java.lang.Long. If this parameter is not set connection requests will not time out (infinite timeout). */ params.setLongParameter("http.conn-manager.timeout", 240000); // 4 minutes params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute httpRoute) { return 10; } }); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, registry); httpClient = new DefaultHttpClient(connManager, params); // // http proxy! // // String proxyHost = "localhost"; // int proxyPort = 8123; // String proxyUsername = null; // String proxyPassword = null; // // final HttpHost hcProxyHost = new HttpHost(proxyHost, proxyPort, "http"); // httpClient.getCredentialsProvider().setCredentials( // new AuthScope(proxyHost, proxyPort), // new UsernamePasswordCredentials(proxyUsername, proxyPassword)); // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hcProxyHost); open = true; }
From source file:org.lol.reddit.cache.CacheManager.java
private CacheManager(final Context context) { if (!isAlreadyInitialized.compareAndSet(false, true)) { throw new RuntimeException("Attempt to initialize the cache twice."); }/*from w ww. j a v a2s . co m*/ this.context = context; dbManager = new CacheDbManager(context); RequestHandlerThread requestHandler = new RequestHandlerThread(); // TODo put somewhere else -- make request specific, no restart needed on prefs change! final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 5); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 25; } }); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encHeader = entity.getContentEncoding(); if (encHeader == null) return; for (final HeaderElement elem : encHeader.getElements()) { if ("gzip".equalsIgnoreCase(elem.getName())) { response.setEntity(new GzipDecompressingEntity(entity)); return; } } } }); downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient); requestHandler.start(); }
From source file:com.ryan.ryanreader.cache.CacheManager.java
private CacheManager(final Context context) { if (!isAlreadyInitialized.compareAndSet(false, true)) { throw new RuntimeException("Attempt to initialize the cache twice."); }/* ww w. ja v a 2 s .c om*/ this.context = context; dbManager = new CacheDbManager(context); requestHandler = new RequestHandlerThread(); // TODo put somewhere else -- make request specific, no restart needed on prefs change! final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 2); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 25; } }); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encHeader = entity.getContentEncoding(); if (encHeader == null) return; for (final HeaderElement elem : encHeader.getElements()) { if ("gzip".equalsIgnoreCase(elem.getName())) { response.setEntity(new GzipDecompressingEntity(entity)); return; } } } }); downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient); requestHandler.start(); for (int i = 0; i < 5; i++) { // TODO remove constant --- customizable final CacheDownloadThread downloadThread = new CacheDownloadThread(downloadQueue, true); downloadThreads.add(downloadThread); } }
From source file:org.esxx.js.protocol.HTTPHandler.java
private static synchronized HttpParams getHttpParams() { if (httpParams == null) { httpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); // No limits ConnManagerParams.setMaxTotalConnections(httpParams, Integer.MAX_VALUE); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return Integer.MAX_VALUE; }//from w w w . jav a 2 s .c o m }); } return httpParams; }
From source file:com.vdisk.net.session.AbstractSession.java
/** * {@inheritDoc} <br/>/*from w w w . ja va 2s . c om*/ * <br/> * The default implementation does all of this and more, including using a * connection pool and killing connections after a timeout to use less * battery power on mobile devices. It's unlikely that you'll want to change * this behavior. */ @Override public synchronized HttpClient getHttpClient() { if (client == null) { // Set up default connection params. There are two routes to // VDisk - api server and content server. HttpParams connParams = new BasicHttpParams(); ConnManagerParams.setMaxConnectionsPerRoute(connParams, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute route) { return 10; } }); ConnManagerParams.setMaxTotalConnections(connParams, 20); // Set up scheme registry. SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); try { schemeRegistry.register(new Scheme("https", TrustAllSSLSocketFactory.getDefault(), 443)); } catch (Exception e) { } DBClientConnManager cm = new DBClientConnManager(connParams, schemeRegistry); // Set up client params. HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, DEFAULT_TIMEOUT_MILLIS); HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_TIMEOUT_MILLIS); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); HttpProtocolParams.setUserAgent(httpParams, makeUserAgent()); httpParams.setParameter(ClientPNames.HANDLE_REDIRECTS, false); DefaultHttpClient c = new DefaultHttpClient(cm, httpParams) { @Override protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { return new DBKeepAliveStrategy(); } @Override protected ConnectionReuseStrategy createConnectionReuseStrategy() { return new DBConnectionReuseStrategy(); } }; c.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); c.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } } }); c.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); client = c; } return client; }
From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java
static DefaultHttpClient createClient(String userAgent, boolean acceptAllSslCertificates) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); // to make connection pool more fault tolerant ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 10; }//from w w w.jav a 2 s .c om }); HttpConnectionParams.setSoTimeout(params, 20 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, false); HttpProtocolParams.setUserAgent(params, userAgent); SSLSocketFactory sslSocketFactory = null; if (acceptAllSslCertificates) { try { sslSocketFactory = new AcceptAllSocketFactory(); } catch (Exception e) { // omitted } } if (sslSocketFactory == null) { sslSocketFactory = SSLSocketFactory.getSocketFactory(); } SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient client = new DefaultHttpClient(manager, params); return client; }
From source file:com.hp.saas.agm.rest.client.AliRestClient.java
private AliRestClient(String location, String domain, String project, String userName, String password, SessionStrategy sessionStrategy) { if (location == null) { throw new IllegalArgumentException("location==null"); }//from www. ja va 2 s. c om validateProjectAndDomain(domain, project); this.location = location; this.userName = userName; this.password = password; this.domain = domain; this.project = project; this.sessionStrategy = sessionStrategy; //HttpParams params = new BasicHttpParams(); //HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); //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"); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); //httpCLient = new DefaultHttpClient(); //httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schReg), new BasicHttpParams()); //httpClient = new DefaultHttpClient(params); //httpClient = new DefaultHttpClient(); //setTimeout(DEFAULT_CLIENT_TIMEOUT); final int MAX_TOTAL_CONNECTIONS = 20; final int MAX_CONNECTIONS_PER_ROUTE = 20; final int TIMEOUT_CONNECT = 15000; final int TIMEOUT_READ = 15000; SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); HttpParams connManagerParams = new BasicHttpParams(); HttpProtocolParams.setVersion(connManagerParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(connManagerParams, true); /*HttpProtocolParams.setUserAgent(connManagerParams,"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");*/ ConnPerRoute cpr = new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute httpRoute) { return 50; } }; ConnManagerParams.setMaxConnectionsPerRoute(connManagerParams, cpr); ConnManagerParams.setMaxTotalConnections(connManagerParams, MAX_TOTAL_CONNECTIONS); //ConnManagerParams.setMaxConnectionsPerRoute(connManagerParams, new ConnPerRouteBean(MAX_CONNECTIONS_PER_ROUTE)); HttpConnectionParams.setConnectionTimeout(connManagerParams, TIMEOUT_CONNECT); HttpConnectionParams.setSoTimeout(connManagerParams, TIMEOUT_READ); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(connManagerParams, schemeRegistry); httpClient = new DefaultHttpClient(cm, connManagerParams); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); responseFilters = new LinkedList<ResponseFilter>(); responseFilters.add(new IssueTicketFilter()); }
From source file:com.cloudant.mazha.HttpRequests.java
private HttpParams getHttpConnectionParams(CouchConfig config) { BasicHttpParams 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, config.isStaleConnectionCheckingEnabled()); HttpConnectionParams.setConnectionTimeout(params, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(params, config.getSocketTimeout()); HttpConnectionParams.setSocketBufferSize(params, config.getBufferSize()); // 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, config.isHandleRedirectEnabled()); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, this.getUserAgent()); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { @Override//ww w . j a va2s . c o m public int getMaxForRoute(HttpRoute route) { return CONN_PER_ROUT; } }); return params; }