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

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

Introduction

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

Prototype

HttpParams setLongParameter(String str, long j);

Source Link

Usage

From source file:se.kodapan.io.http.HttpAccessor.java

public synchronized void open() throws IOException {

    if (open) {/*from w ww.  j  a  va2  s .c  o m*/
        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;

}