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

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

Introduction

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

Prototype

public static void setSoTimeout(HttpParams httpParams, int i) 

Source Link

Usage

From source file:com.liuguangqiang.download.core.AsyncDownload.java

private void init() {
    mDownloadQueue = new DownloadQueue();
    executorService = mConfiguration.executorService;
    mHttpClient = AndroidHttpClient.newInstance(Constants.USER_AGENT);
    mHttpParams = mHttpClient.getParams();
    mHttpParams.setParameter(ClientPNames.HANDLE_REDIRECTS, true);
    HttpConnectionParams.setConnectionTimeout(mHttpParams, mConfiguration.connectionTimeout);
    HttpConnectionParams.setSoTimeout(mHttpParams, mConfiguration.socketTimeout);
}

From source file:at.alladin.rmbt.client.helper.JSONParser.java

public JSONObject getURL(final URI uri) {
    JSONObject jObj = null;/*ww w .  ja va 2s .c  o m*/
    String responseBody;

    try {
        final HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 20000);
        HttpConnectionParams.setSoTimeout(params, 20000);
        final HttpClient client = new DefaultHttpClient(params);

        final HttpGet httpget = new HttpGet(uri);

        final ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseBody = client.execute(httpget, responseHandler);

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(responseBody);
        } catch (final JSONException e) {
            writeErrorList("Error parsing JSON " + e.toString());
        }

    } catch (final UnsupportedEncodingException e) {
        writeErrorList("Wrong encoding");
        // e.printStackTrace();
    } catch (final HttpResponseException e) {
        writeErrorList(
                "Server responded with Code " + e.getStatusCode() + " and message '" + e.getMessage() + "'");
    } catch (final ClientProtocolException e) {
        writeErrorList("Wrong Protocol");
        // e.printStackTrace();
    } catch (final IOException e) {
        writeErrorList("IO Exception");
        e.printStackTrace();
    }

    // return JSONObject
    return jObj;
}

From source file:org.camunda.bpm.AbstractWebappIntegrationTest.java

@Before
public void createClient() throws Exception {
    testProperties = new TestProperties();

    String applicationContextPath = getApplicationContextPath();
    APP_BASE_PATH = testProperties.getApplicationPath("/" + applicationContextPath);
    LOGGER.info("Connecting to application " + APP_BASE_PATH);

    ClientConfig clientConfig = new DefaultApacheHttpClient4Config();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    client = ApacheHttpClient4.create(clientConfig);

    defaultHttpClient = (DefaultHttpClient) client.getClientHandler().getHttpClient();
    HttpParams params = defaultHttpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 3 * 60 * 1000);
    HttpConnectionParams.setSoTimeout(params, 10 * 60 * 1000);
}

From source file:org.apache.camel.component.http4.HttpPollingConsumer.java

protected Exchange doReceive(int timeout) {
    Exchange exchange = endpoint.createExchange();
    HttpRequestBase method = createMethod(exchange);

    // set optional timeout in millis
    if (timeout > 0) {
        HttpConnectionParams.setSoTimeout(method.getParams(), timeout);
    }/*from  w  w w .ja  va 2 s.c  om*/

    HttpEntity responeEntity = null;
    try {
        // execute request
        HttpResponse response = httpClient.execute(method);
        int responseCode = response.getStatusLine().getStatusCode();
        responeEntity = response.getEntity();
        Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);

        // lets store the result in the output message.
        Message message = exchange.getOut();
        message.setBody(body);

        // lets set the headers
        Header[] headers = response.getAllHeaders();
        HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
        for (Header header : headers) {
            String name = header.getName();
            // mapping the content-type
            if (name.toLowerCase().equals("content-type")) {
                name = Exchange.CONTENT_TYPE;
            }
            String value = header.getValue();
            if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
                message.setHeader(name, value);
            }
        }
        message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);

        return exchange;
    } catch (IOException e) {
        throw new RuntimeCamelException(e);
    } finally {
        if (responeEntity != null) {
            try {
                EntityUtils.consume(responeEntity);
            } catch (IOException e) {
                // nothing what we can do
            }
        }
    }
}

From source file:net.sarangnamu.common.network.BkHttp.java

public void setTimeout(int connTimeout, int socketTimeout) {
    if (http == null) {
        return;/*from   w  w  w  . j  av  a 2  s  .  com*/
    }

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, connTimeout);
    HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);

    http.setParams(httpParameters);
}

From source file:com.core.ServerConnector.java

public static String GET(String endpoint, Map<String, String> params, Map<String, String> header)
        throws Exception {
    String result = null;//from w ww .j ava 2 s  .  c o  m
    StringBuilder bodyBuilder = new StringBuilder(endpoint).append("?");
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    //Log.i("SCANTRANX", "URL:"+body);
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpClient client = new DefaultHttpClient(httpParameters);
    HttpGet request = new HttpGet(body);

    for (Entry<String, String> mm : header.entrySet()) {
        request.addHeader(mm.getKey(), mm.getValue());
    }
    System.out.println("Raw Request:" + request);
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        result = SystemUtil.convertStreamToString(instream);
        instream.close();
    }

    return result;
}

From source file:com.flicklib.service.HttpClientSourceLoader.java

@Inject
public HttpClientSourceLoader(@Named(value = Constants.HTTP_TIMEOUT) final Integer timeout) {

    ThreadSafeClientConnManager tm = new ThreadSafeClientConnManager();
    tm.setDefaultMaxPerRoute(20);/* w ww  .j av  a2s  .co  m*/
    tm.setMaxTotal(200);
    client = new DefaultHttpClient(tm);

    if (timeout != null) {
        // wait max x sec
        HttpConnectionParams.setSoTimeout(client.getParams(), timeout);
    }
}

From source file:net.peterkuterna.android.apps.devoxxsched.util.SyncUtils.java

/**
 * Generate and return a {@link HttpClient} configured for general use,
 * including setting an application-specific user-agent string.
 *//*from www . j a  v a2 s.  co  m*/
public static HttpClient getHttpClient(Context context) {
    final HttpParams params = new BasicHttpParams();

    // Use generous timeouts for slow mobile networks
    HttpConnectionParams.setConnectionTimeout(params, 200 * SECOND_IN_MILLIS);
    HttpConnectionParams.setSoTimeout(params, 200 * SECOND_IN_MILLIS);

    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpProtocolParams.setUserAgent(params, buildUserAgent(context));

    final HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    final SchemeRegistry schemeReg = new SchemeRegistry();
    schemeReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeReg.register(new Scheme("https", sslSocketFactory, 443));
    final ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, schemeReg);
    final DefaultHttpClient client = new DefaultHttpClient(connectionManager, params);

    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) {
            // Add header to accept gzip content
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
        }
    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(HttpResponse response, HttpContext context) {
            // Inflate any responses compressed with gzip
            final HttpEntity entity = response.getEntity();
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    return client;
}

From source file:com.base.httpclient.HttpJsonClient.java

/**
 * httpClient/*from w  w  w  . jav a 2 s . c  om*/
 * @return
 */
public static DefaultHttpClient getHttpClient() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    HttpParams httpParams = new BasicHttpParams();
    cm.setMaxTotal(10);//   
    cm.setDefaultMaxPerRoute(5);// ? 
    HttpConnectionParams.setConnectionTimeout(httpParams, 60000);//
    HttpConnectionParams.setSoTimeout(httpParams, 60000);//?
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUseExpectContinue(httpParams, false);
    httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams);
    //httpClient.setCookieStore(null);
    httpClient.getCookieStore().clear();
    httpClient.getCookieStore().getCookies().clear();
    //   httpClient.setHttpRequestRetryHandler(new HttpJsonClient().new HttpRequestRetry());//?
    return httpClient;
}

From source file:ch.ethz.dcg.pancho3.view.youtube.Query.java

private void initializeHttpClient() {
    // Initialize Http client
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);
    httpClient = new DefaultHttpClient(params);

    HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(2, true);
    httpClient.setHttpRequestRetryHandler(retryHandler);
}