Example usage for org.apache.http.params HttpConnectionParams setConnectionTimeout

List of usage examples for org.apache.http.params HttpConnectionParams setConnectionTimeout

Introduction

In this page you can find the example usage for org.apache.http.params HttpConnectionParams setConnectionTimeout.

Prototype

public static void setConnectionTimeout(HttpParams httpParams, int i) 

Source Link

Usage

From source file:fast.simple.download.http.DownloadHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * //from  www.  ja  va 2  s . com
 * @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 DownloadHttpClient 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, true);

    // 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 DownloadHttpClient(manager, params);
}

From source file:com.volley.toolbox.HttpClientStack.java

/**
 * //from  ww w .  j  a  v  a2 s  .  co  m
 * @param request the request to perform
 * @param additionalHeaders additional headers to be sent together with
 *         {@link Request#getHeaders()}
 * @return
 * @throws IOException
 * @throws AuthFailureError
 */
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    //request ??httprequest? httpurlrequest
    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    return mClient.execute(httpRequest);
}

From source file:org.jupnp.transport.impl.apache.StreamClientImpl.java

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", 80, PlainSocketFactory.getSocketFactory()));

    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));
    }/*from   w  ww .j  ava  2  s. c  om*/
}

From source file:me.xiaopan.sketch.http.HttpClientStack.java

public HttpClientStack() {
    BasicHttpParams httpParams = new BasicHttpParams();
    ConnManagerParams.setTimeout(httpParams, DEFAULT_WAIT_TIMEOUT);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams,
            new ConnPerRouteBean(DEFAULT_MAX_ROUTE_CONNECTIONS));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSoTimeout(httpParams, readTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
    httpClient.addRequestInterceptor(new GzipProcessRequestInterceptor());
    httpClient.addResponseInterceptor(new GzipProcessResponseInterceptor());
}

From source file:com.google.appengine.tck.endpoints.support.EndPointClient.java

/**
 * Get client./*w w  w  .  j a  v  a 2s . c  o  m*/
 *
 * @return the client
 */
private synchronized HttpClient getClient() {
    if (client == null) {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, getConnectionTimeout());
        HttpProtocolParams.setVersion(params, getHttpVersion());
        HttpProtocolParams.setContentCharset(params, getContentCharset());

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", getPort(), getPlainFactory()));
        schemeRegistry.register(new Scheme("https", getSslPort(), getSslFactory()));

        ClientConnectionManager ccm = createClientConnectionManager(schemeRegistry);

        client = createClient(ccm, params);
    }

    return client;
}

From source file:org.teleal.cling.transport.impl.apache.StreamClientImpl.java

public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException {
    this.configuration = configuration;

    ConnManagerParams.setMaxTotalConnections(globalParams, getConfiguration().getMaxTotalConnections());
    HttpConnectionParams.setConnectionTimeout(globalParams,
            getConfiguration().getConnectionTimeoutSeconds() * 1000);
    HttpConnectionParams.setSoTimeout(globalParams, getConfiguration().getDataReadTimeoutSeconds() * 1000);
    HttpProtocolParams.setContentCharset(globalParams, getConfiguration().getContentCharset());
    if (getConfiguration().getSocketBufferSize() != -1) {

        // Android configuration will set this to 8192 as its httpclient is
        // based//  ww  w.ja v a 2 s .c o m
        // on a random pre 4.0.1 snapshot whose BasicHttpParams do not set a
        // default value for socket buffer size.
        // This will also avoid OOM on the HTC Thunderbolt where default
        // size is 2Mb (!):
        // http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt

        HttpConnectionParams.setSocketBufferSize(globalParams, getConfiguration().getSocketBufferSize());
    }
    HttpConnectionParams.setStaleCheckingEnabled(globalParams, getConfiguration().getStaleCheckingEnabled());

    // This is a pretty stupid API...
    // https://issues.apache.org/jira/browse/HTTPCLIENT-805
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // The
    // 80
    // here
    // is...
    // useless
    clientConnectionManager = new ThreadSafeClientConnManager(globalParams, registry);
    httpClient = new DefaultHttpClient(clientConnectionManager, globalParams);
    if (getConfiguration().getRequestRetryCount() != -1) {
        httpClient.setHttpRequestRetryHandler(
                new DefaultHttpRequestRetryHandler(getConfiguration().getRequestRetryCount(), false));
    }

    /*
     * // TODO: Ugh! And it turns out that by default it doesn't even use
     * persistent connections properly!
     * 
     * @Override protected ConnectionReuseStrategy
     * createConnectionReuseStrategy() { return new
     * NoConnectionReuseStrategy(); }
     * 
     * @Override protected ConnectionKeepAliveStrategy
     * createConnectionKeepAliveStrategy() { return new
     * ConnectionKeepAliveStrategy() { public long
     * getKeepAliveDuration(HttpResponse httpResponse, HttpContext
     * httpContext) { return 0; } }; }
     * httpClient.removeRequestInterceptorByClass(RequestConnControl.class);
     */
}

From source file:com.hotstar.player.adplayer.utils.http.AsyncHttpConnection.java

public void run() {
    _handler.sendMessage(Message.obtain(_handler, AsyncHttpConnection.DID_START));

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, _timeout);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for _data.
    HttpConnectionParams.setSoTimeout(httpParameters, _timeout);

    _httpClient = new DefaultHttpClient(httpParameters);
    try {/* ww  w  . ja v  a  2 s. c  om*/
        HttpResponse response = null;
        switch (_method) {
        case GET:
            HttpGet httpGet = new HttpGet(_url);
            for (Map.Entry<String, String> header : _headers.entrySet()) {
                httpGet.setHeader(header.getKey(), header.getValue());
            }
            response = _httpClient.execute(httpGet);
            break;
        case POST:
            HttpPost httpPost = new HttpPost(_url);
            for (Map.Entry<String, String> header : _headers.entrySet()) {
                httpPost.setHeader(header.getKey(), header.getValue());
            }
            httpPost.setEntity(new StringEntity(_data));
            response = _httpClient.execute(httpPost);
            break;
        default:
            throw new IllegalArgumentException("Unsupported HTTP method: " + _method.name());
        }

        processEntity(response.getEntity());
    } catch (Exception e) {
        _handler.sendMessage(Message.obtain(_handler, AsyncHttpConnection.DID_ERROR, e));
    }
    ConnectionManager.getInstance().didComplete(this);
}

From source file:org.devtcg.five.util.streaming.FailfastHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * @param userAgent to report in your HTTP requests.
 * @return FailfastHttpClient for you to use for all your requests.
 *///from ww w. ja  va  2  s  . co  m
public static FailfastHttpClient newInstance(String userAgent) {
    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);

    // Default connection and socket timeout of 10 seconds.  Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, READ_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);

    // Set the specified user agent and register standard protocols.
    if (userAgent != null)
        HttpProtocolParams.setUserAgent(params, userAgent);

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

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new FailfastHttpClient(manager, params);
}