Example usage for org.apache.http.params CoreConnectionPNames SO_TIMEOUT

List of usage examples for org.apache.http.params CoreConnectionPNames SO_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.http.params CoreConnectionPNames SO_TIMEOUT.

Prototype

String SO_TIMEOUT

To view the source code for org.apache.http.params CoreConnectionPNames SO_TIMEOUT.

Click Source Link

Usage

From source file:com.almende.reaal.apachehttp.ApacheHttpClient.java

/**
 * Instantiates a new apache http client.
 *
 *//* w w w .j  a v a 2 s. co m*/
private ApacheHttpClient() {

    // Allow self-signed SSL certificates:
    final TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
    final X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

    SSLSocketFactory sslSf;
    try {
        sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme https = new Scheme("https", 443, sslSf);
        schemeRegistry.register(https);
    } catch (Exception e) {
        LOG.warning("Couldn't init SSL socket, https not supported!");
    }

    // Work with PoolingClientConnectionManager
    final ClientConnectionManager connection = new PoolingClientConnectionManager(schemeRegistry);

    // Provide eviction thread to clear out stale threads.
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        wait(5000);
                        connection.closeExpiredConnections();
                        connection.closeIdleConnections(30, TimeUnit.SECONDS);
                    }
                }
            } catch (final InterruptedException ex) {
            }
        }
    }).start();

    // generate httpclient
    httpClient = new DefaultHttpClient(connection);

    final HttpParams params = httpClient.getParams();

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
    httpClient.setParams(params);
}

From source file:cn.edu.szjm.support.http.IgnitedHttpRequestBase.java

public IgnitedHttpRequest withTimeout(int timeout) {
    oldSocketTimeout = httpClient.getParams().getIntParameter(CoreConnectionPNames.SO_TIMEOUT,
            IgnitedHttp.DEFAULT_SOCKET_TIMEOUT);
    oldConnTimeout = httpClient.getParams().getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            IgnitedHttp.DEFAULT_WAIT_FOR_CONNECTION_TIMEOUT);

    ignitedHttp.setSocketTimeout(timeout);
    ignitedHttp.setConnectionTimeout(timeout);

    timeoutChanged = true;/*from   www.  ja v a 2  s  .com*/
    return this;
}

From source file:utils.httpUtil_inThread.java

/**
 * ?// www  .j a v  a2s .c o m
 *
 * @return ?null
 */
public String HttpGetString(String website, String cookie, int timeout_connection, int timeout_read) {
    String result;
    try {
        String strHTTP = "http://";
        if (website != null && !website.contains(strHTTP))
            website = strHTTP + website; //?http://

        //1.?
        HttpClient client = new DefaultHttpClient();
        //2.?
        HttpGet httpGet = new HttpGet(website);

        if (cookie != null && !cookie.equals(""))
            httpGet.setHeader("Cookie", cookie);

        //??
        if (timeout_connection <= 1000)
            timeout_connection = timeout_text_connection;
        if (timeout_read < 1000)
            timeout_read = timeout_text_read;

        //HttpClient
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout_connection);
        //HttpClient?
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout_read);

        //3.??GET
        HttpResponse response = client.execute(httpGet);

        //4.??
        StringBuilder sb = new StringBuilder();
        HttpEntity entity = response.getEntity();

        //5.??
        String strEncoding = entity.getContentType().getValue();
        strEncoding = strEncoding.substring(strEncoding.indexOf("charset="));
        strEncoding = strEncoding.replace("charset=", "");

        //6.???
        int ResponseCode = response.getStatusLine().getStatusCode();
        if (ResponseCode == 200) {
            BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), strEncoding));
            String temp;
            while ((temp = br.readLine()) != null) {
                String str = new String(temp.trim().getBytes());
                sb.append(str);
                sb.append("\r\n");
            }
            br.close();

            //???
            result = new String(sb);
        } else {
            //ResponseCode?200
            result = "";
            abstract_LogUtil.i(this, "[ResponseCode]" + ResponseCode);
        }
    } catch (Exception e) {
        result = "";
        abstract_LogUtil.e(this, "[?]" + website);
        abstract_LogUtil.e(this, "[GetHttp]" + e.toString());
    }
    return result;
}

From source file:com.terremark.exception.LeakTest.java

/**
 * TODO//from   w w w .j a v a 2  s. c o  m
 *
 * @return TODO
 */
private static DefaultHttpClient getHttpClient() {
    final HttpParams params = new SyncBasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 1000);

    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
    final ThreadSafeClientConnManager httpConnectionManager = new ThreadSafeClientConnManager(schemeRegistry);

    httpConnectionManager.setMaxTotal(3);
    httpConnectionManager.setDefaultMaxPerRoute(3);

    return new DefaultHttpClient(httpConnectionManager, params);
}

From source file:com.amalto.workbench.utils.HttpClientUtil.java

private static DefaultHttpClient createClient() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIMEOUT);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    return new DefaultHttpClient(cm, params);
}

From source file:net.kungfoo.grizzly.proxy.impl.Activator.java

private static void setupClient() throws IOReactorException {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);

    connectingIOReactor = new DefaultConnectingIOReactor(1, params);

    BasicHttpProcessor originServerProc = new BasicHttpProcessor();
    originServerProc.addInterceptor(new RequestContent());
    originServerProc.addInterceptor(new RequestTargetHost());
    originServerProc.addInterceptor(new RequestConnControl());
    originServerProc.addInterceptor(new RequestUserAgent());
    originServerProc.addInterceptor(new RequestExpectContinue());

    NHttpClientHandler connectingHandler = new ConnectingHandler(originServerProc,
            new DefaultConnectionReuseStrategy(), params);

    connectingEventDispatch = new DefaultClientIOEventDispatch(connectingHandler, params);
}

From source file:org.jboss.arquillian.android.drone.impl.SelendroidHelper.java

/**
 * Waits for Selendroid start. After installation and execution of instrumentation command, we repeatedly send http request
 * to status page to get response code of 200 - server is up and running and we can proceed safely.
 *///from w  w  w.ja  va2  s  .c  o m
public void waitForServerHTTPStart() {
    HttpClient httpClient = new DefaultHttpClient();

    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIME_OUT_SECONDS * 1000);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            CONNECTION_TIME_OUT_SECONDS * 1000);

    HttpGet httpGet = new HttpGet(getSelendroidStatusURI());

    boolean connectionSuccess = false;

    for (int i = NUM_CONNECTION_RETIRES; i > 0; i--) {
        try {
            HttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                connectionSuccess = true;
                break;
            }
            logger.log(Level.INFO,
                    "Response was not 200, response was: " + statusCode + ". Repeating " + i + " times.");
        } catch (ClientProtocolException e) {
            logger.log(Level.WARNING, e.getMessage());
        } catch (IOException e) {
            logger.log(Level.WARNING, e.getMessage());
        }
    }

    httpClient.getConnectionManager().shutdown();

    if (!connectionSuccess) {
        throw new AndroidExecutionException("Unable to get successful connection from Selendroid http server.");
    }
}

From source file:com.sunhz.projectutils.httputils.HttpClientUtils.java

/**
 * post access to the network, you need to use in the sub-thread
 *
 * @param url    url//www .jav  a  2s.  com
 * @param params params
 * @return response inputStream
 * @throws IOException In the case of non-200 status code is returned, it would have thrown
 */
public static InputStream postInputStream(String url, Map<String, String> params) throws IOException {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entity);
    org.apache.http.client.HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Constant.TimeInApplication.NET_TIMEOUT);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TimeInApplication.NET_TIMEOUT);
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        return response.getEntity().getContent();
    } else {
        throw new IllegalArgumentException("postInputStreamInSubThread response status code is "
                + response.getStatusLine().getStatusCode());
    }

}