Example usage for org.apache.http.params HttpParams setIntParameter

List of usage examples for org.apache.http.params HttpParams setIntParameter

Introduction

In this page you can find the example usage for org.apache.http.params HttpParams setIntParameter.

Prototype

HttpParams setIntParameter(String str, int i);

Source Link

Usage

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * client?Header?Cookie/*from ww  w  . j  a  v  a  2s  .co  m*/
 * @param aconfig
 * @param cookies
 */
public void init(Site _site) {
    //HTTP?
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages())
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    httpClient.getParams().setIntParameter("http.socket.timeout", 60000);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, config.isFollowRedirects());
    //      HttpClientParams.setCookiePolicy(httpClient.getParams(),CookiePolicy.BEST_MATCH);

    //?
    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    //?GZIP
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    if (_site != null) {
        this.site = _site;
        if (this.site.getHeaders() != null && this.site.getHeaders().getHeader() != null) {
            for (org.eweb4j.spiderman.xml.Header header : this.site.getHeaders().getHeader()) {
                this.addHeader(header.getName(), header.getValue());
            }
        }
        if (this.site.getCookies() != null && this.site.getCookies().getCookie() != null) {
            for (org.eweb4j.spiderman.xml.Cookie cookie : this.site.getCookies().getCookie()) {
                this.addCookie(cookie.getName(), cookie.getValue(), cookie.getHost(), cookie.getPath());
            }
        }
    }
}

From source file:SandBox.testing.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SSLContext sslContext = null;
    try {/*from ww  w. ja  va  2 s  .  c  o m*/
        sslContext = SSLContext.getInstance("SSL");
        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    SSLSocketFactory sf = new SSLSocketFactory(sslContext);
    Scheme httpsScheme = new Scheme("https", 443, sf);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);

    //SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }

    connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:org.mulgara.scon.Connection.java

/**
 * Establish a client connection in HTTP
 * @return A new client connection with parameters set for this object
 *//*from w  w w.j  av a2  s  .c  o  m*/
private HttpClient getHttpClient() {
    HttpParams params = new BasicHttpParams();
    params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, expectContinue);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
    return new DefaultHttpClient(conManager, params);
}

From source file:com.swater.meimeng.activity.oomimg.ImageCache.java

/**
 * Gets an instance of AndroidHttpClient if the devices has it (it was introduced in 2.2), or
 * falls back on a http client that should work reasonably well.
 *
 * @return a working instance of an HttpClient
 *///from  w w  w.  j  a va 2  s.c  o m
private HttpClient getHttpClient() {
    HttpClient ahc;
    try {
        final Class<?> ahcClass = Class.forName("android.net.http.AndroidHttpClient");
        final Method newInstance = ahcClass.getMethod("newInstance", String.class);
        ahc = (HttpClient) newInstance.invoke(null, "ImageCache");

    } catch (final ClassNotFoundException e) {
        DefaultHttpClient dhc = new DefaultHttpClient();
        final HttpParams params = dhc.getParams();
        dhc = null;

        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20 * 1000);

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

        final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
        ahc = new DefaultHttpClient(manager, params);

    } catch (final NoSuchMethodException e) {

        final RuntimeException re = new RuntimeException("Programming error");
        re.initCause(e);
        throw re;

    } catch (final IllegalAccessException e) {
        final RuntimeException re = new RuntimeException("Programming error");
        re.initCause(e);
        throw re;

    } catch (final InvocationTargetException e) {
        final RuntimeException re = new RuntimeException("Programming error");
        re.initCause(e);
        throw re;
    }
    return ahc;
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.HCAPAdapter.java

/**
 * Sets an int parameter on the http client, use null to clear
 * // w w w.  j a v a  2  s. c  o  m
 * @param timeout
 *            The new timeout
 */
protected void setIntClientParameter(final Integer timeout, final String parameter) {
    HttpParams params = httpClient.getParams();
    if (timeout != null) {
        params.setIntParameter(parameter, timeout);
    } else {
        params.removeParameter(parameter);
    }
    httpClient.setParams(params);
}

From source file:net.elasticgrid.rackspace.common.RackspaceConnection.java

private void configureHttpClient() {
    HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, userAgent);
    HttpProtocolParams.setUseExpectContinue(params, true);

    //        params.setBooleanParameter("http.tcp.nodelay", true);
    //        params.setBooleanParameter("http.coonection.stalecheck", false);
    ConnManagerParams.setTimeout(params, getConnectionManagerTimeout());
    ConnManagerParams.setMaxTotalConnections(params, getMaxConnections());
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(getMaxConnections()));
    params.setIntParameter("http.socket.timeout", getSoTimeout());
    params.setIntParameter("http.connection.timeout", getConnectionTimeout());

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
    hc = new DefaultHttpClient(connMgr, params);

    ((DefaultHttpClient) hc).addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }/*from   w w  w.j av  a  2s .  c  o  m*/
        }
    });
    ((DefaultHttpClient) hc).addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity == null)
                return;
            Header ceHeader = entity.getContentEncoding();
            if (ceHeader != null) {
                for (HeaderElement codec : ceHeader.getElements()) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    if (proxyHost != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        hc.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        logger.info("Proxy Host set to " + proxyHost + ":" + proxyPort);
    }
}

From source file:org.bedework.util.http.BasicHttpClient.java

/**
 * @param host default host or null/*from w  w w.  java2 s. c o  m*/
 * @param port default port if host supplied or -1 for default
 * @param scheme default scheme if host supplied or null for default
 * @param timeOut - millisecs, 0 for no timeout
 * @param followRedirects true for auto handling
 * @throws HttpException
 */
public BasicHttpClient(final String host, final int port, final String scheme, final int timeOut,
        final boolean followRedirects) throws HttpException {
    super(connManager, null);
    setKeepAliveStrategy(kas);

    if (sslDisabled) {
        warn("*******************************************************");
        warn(" SSL disabled");
        warn("*******************************************************");
    }

    final HttpParams params = getParams();

    if (host != null) {
        hostSpecified = true;
        final HttpHost httpHost = new HttpHost(host, port, scheme);
        params.setParameter(ClientPNames.DEFAULT_HOST, httpHost);
    }

    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut);

    // XXX Should have separate value for this.
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeOut * 2);

    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
}

From source file:com.pmi.restlet.ext.httpclient.HttpClientHelper.java

/**
 * Configures the various parameters of the connection manager and the HTTP
 * client.// w w  w. java 2  s.  com
 *
 * @param params
 *            The parameter list to update.
 */
protected void configure(HttpParams params) {
    ConnManagerParams.setMaxTotalConnections(params, getMaxTotalConnections());
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(getMaxConnectionsPerHost()));

    // Configure other parameters
    HttpClientParams.setAuthenticating(params, false);
    HttpClientParams.setRedirecting(params, isFollowRedirects());
    HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
    HttpConnectionParams.setTcpNoDelay(params, getTcpNoDelay());
    HttpConnectionParams.setConnectionTimeout(params, getConnectTimeout());
    HttpConnectionParams.setSoTimeout(params, getSocketTimeout());
    params.setIntParameter(ClientPNames.MAX_REDIRECTS, getMaxRedirects());

    //-Dhttp.proxyHost=chlaubc.obs.pmi -Dhttp.proxyPort=8000 -Dhttp.nonProxyHosts=localhost|127.0.0.1|*.app.pmi
    String httpProxyHost = getProxyHost();
    if (httpProxyHost != null) {
        if (StringUtils.isNotEmpty(getNonProxyHosts())) {
            System.setProperty("http.nonProxyHosts", getNonProxyHosts());
        } else {
            System.getProperties().remove("http.nonProxyHosts");
        }
        System.setProperty("http.proxyPort", String.valueOf(getProxyPort()));
        System.setProperty("http.proxyHost", httpProxyHost);
        HttpHost proxy = new HttpHost(httpProxyHost, getProxyPort());
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    }
}