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:eu.trentorise.smartcampus.ac.network.RemoteConnector.java

private static HttpClient getHttpClient() {
    HttpClient httpClient = new DefaultHttpClient();
    final HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    HttpConnectionParams.setSoTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS);

    // return httpClient;
    return HttpsClientBuilder.getNewHttpClient(params);
}

From source file:com.antorofdev.util.Http.java

/**
 * Performs http GET petition to server.
 *
 * @param url        URL to perform GET petition.
 * @param parameters Parameters to include in petition.
 *
 * @return Response from the server.//from   w  ww. j a v  a 2s  .co  m
 * @throws IOException If the <tt>parameters</tt> have errors, connection timmed out,
 *                     socket timmed out or other error related with the connection occurs.
 */
public static HttpResponse get(String url, ArrayList<NameValuePair> parameters) throws IOException {
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 10000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);

    if (parameters != null) {
        String paramString = URLEncodedUtils.format(parameters, "utf-8");
        url += paramString;
    }

    HttpGet httpget = new HttpGet(url);

    HttpResponse response = httpclient.execute(httpget);

    return response;
}

From source file:com.riksof.a320.http.CoreHttpClient.java

/**
 * This method is used to execute Get Http Request
 * /*from  w  w w.  j  av a  2  s  .  co  m*/
 * @param url
 * @return
 * @throws ServerException
 */
public String executeGet(String url) throws ServerException {

    // Response String
    String responseString = null;

    headers = new HashMap<String, String>();

    try {

        CacheConfig cacheConfig = new CacheConfig();
        cacheConfig.setMaxCacheEntries(1000);
        cacheConfig.setMaxObjectSizeBytes(1024 * 1024);

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);

        // Create http client object
        DefaultHttpClient realClient = new DefaultHttpClient(httpParameters);
        realClient.addResponseInterceptor(MakeCacheable.INSTANCE, 0);
        CachingHttpClient httpclient = new CachingHttpClient(realClient, cacheConfig);

        // Create http context object
        HttpContext localContext = new BasicHttpContext();

        // Check for cached data against URL
        if (Cache.getInstance().get(url) == null) {

            // Execute HTTP Get Request
            HttpGet httpget = new HttpGet(url);

            // Get http response
            HttpResponse response = httpclient.execute(httpget, localContext);

            for (Header h : response.getAllHeaders()) {
                headers.put(h.getName(), h.getValue());
            }

            // Create http entity object
            HttpEntity entity = response.getEntity();

            // Create and convert stream into string
            InputStream inStream = entity.getContent();
            responseString = convertStreamToString(inStream);

            // Cache url data
            Cache.getInstance().put(url, responseString);

        } else {

            // Returned cached data
            responseString = Cache.getInstance().get(url);
        }

    } catch (ClientProtocolException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ClientProtocolException");
    } catch (ConnectTimeoutException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("ConnectTimeoutException");
    } catch (IOException e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Request Timeout");
    } catch (Exception e) {
        // throw custom server exception in case of Exception
        e.printStackTrace();
        throw new ServerException("Exception");
    } finally {
    }
    return responseString;
}

From source file:com.shwy.bestjoy.utils.AndroidHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 *
 * @param userAgent to report in your HTTP requests.
 * @return AndroidHttpClient for you to use for all your requests.
 *///  www .j  a  v  a2  s . c  o m
public static HttpClient newInstance(String userAgent) {

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sslSocketFactory = new SSLSocketFactoryEx(trustStore);
        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        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 20 seconds.  Tweak to taste.
        HttpConnectionParams.setConnectionTimeout(params, 60 * 1000);
        HttpConnectionParams.setSoTimeout(params, 60 * 1000);
        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);

        // Set the specified user agent and register standard protocols.
        HttpProtocolParams.setUserAgent(params, userAgent);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 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 AndroidHttpClient(manager, params);
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    return new DefaultHttpClient();

}

From source file:com.windigo.http.client.ApacheHttpClient.java

/**
 * Create {@link HttpParams} for {@link DefaultHttpClient}
 * /*www. j  a  va  2  s  .c  om*/
 * @return {@link HttpParams}
 */
private final static HttpParams createHttpParams() {
    final HttpParams params = new BasicHttpParams();

    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, GlobalSettings.CONNNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, GlobalSettings.CONNNECTION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    return params;
}

From source file:devza.app.android.droidnetkey.UsageAction.java

public UsageAction(Context context, TextView usage, TextView cost) {
    this.context = context;
    this.cost = cost;
    this.usage = usage;

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

    //HTTP Paramaters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, TIMEOUT);
    SingleClientConnManager cm = new SingleClientConnManager(params, registry);

    this.client = new DefaultHttpClient(cm, params);

}

From source file:com.lonepulse.robozombie.executor.ConfigurationService.java

/**
 * <p>The <i>out-of-the-box</i> configuration for an instance of {@link HttpClient} which will be used for 
 * executing all endpoint requests. Below is a detailed description of all configured properties.</p> 
 * <br>/* ww w .  ja  v a  2  s. c o  m*/
 * <ul>
 * <li>
 * <p><b>HttpClient</b></p>
 * <br>
 * <p>It registers two {@link Scheme}s:</p>
 * <br>
 * <ol>
 *    <li><b>HTTP</b> on port <b>80</b> using sockets from {@link PlainSocketFactory#getSocketFactory}</li>
 *    <li><b>HTTPS</b> on port <b>443</b> using sockets from {@link SSLSocketFactory#getSocketFactory}</li>
 * </ol>
 * 
 * <p>It uses a {@link ThreadSafeClientConnManager} with the following parameters:</p>
 * <br>
 * <ol>
 *    <li><b>Redirecting:</b> enabled</li>
 *    <li><b>Connection Timeout:</b> 30 seconds</li>
 *    <li><b>Socket Timeout:</b> 30 seconds</li>
 *    <li><b>Socket Buffer Size:</b> 12000 bytes</li>
 *    <li><b>User-Agent:</b> via <code>System.getProperty("http.agent")</code></li>
 * </ol>
 * </li>
 * </ul>
 * @return the instance of {@link HttpClient} which will be used for request execution
 * <br><br>
 * @since 1.3.0
 */
@Override
public Configuration getDefault() {

    return new Configuration() {

        @Override
        public HttpClient httpClient() {

            try {

                HttpParams params = new BasicHttpParams();
                HttpClientParams.setRedirecting(params, true);
                HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);
                HttpConnectionParams.setSoTimeout(params, 30 * 1000);
                HttpConnectionParams.setSocketBufferSize(params, 12000);
                HttpProtocolParams.setUserAgent(params, System.getProperty("http.agent"));

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

                ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

                return new DefaultHttpClient(manager, params);
            } catch (Exception e) {

                throw new ConfigurationFailedException(e);
            }
        }
    };
}

From source file:org.jfrog.build.client.PreemptiveHttpClient.java

private DefaultHttpClient createHttpClient(String userName, String password, int timeout) {
    BasicHttpParams params = new BasicHttpParams();
    int timeoutMilliSeconds = timeout * 1000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutMilliSeconds);
    HttpConnectionParams.setSoTimeout(params, timeoutMilliSeconds);
    DefaultHttpClient client = new DefaultHttpClient(params);

    if (userName != null && !"".equals(userName)) {
        client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(userName, password));
        localContext = new BasicHttpContext();

        // Generate BASIC scheme object and stick it to the local execution context
        BasicScheme basicAuth = new BasicScheme();
        localContext.setAttribute("preemptive-auth", basicAuth);

        // Add as the first request interceptor
        client.addRequestInterceptor(new PreemptiveAuth(), 0);
    }/*  ww w  .  j a  va 2  s.  co m*/
    boolean requestSentRetryEnabled = Boolean.parseBoolean(System.getProperty("requestSentRetryEnabled"));
    if (requestSentRetryEnabled) {
        client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, requestSentRetryEnabled));
    }
    // set the following user agent with each request
    String userAgent = "ArtifactoryBuildClient/" + CLIENT_VERSION;
    HttpProtocolParams.setUserAgent(client.getParams(), userAgent);
    return client;
}

From source file:com.adeven.adjustio.RequestHandler.java

private void initInternal() {
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);
    httpClient = new DefaultHttpClient(httpParams);
}

From source file:com.overture.questdroid.utility.ExtHttpClientStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);/*from  w w w  . j  a  v a  2 s .c o  m*/
    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);

    HttpResponse resp = mClient.execute(httpRequest);

    return convertResponseNewToOld(resp);
}