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:es.trigit.gitftask.Conexion.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 ww w.  j  av a  2s. co  m
 * @throws IOException If the <tt>parameters</tt> have errors, connection timed out,
 *                     socket timed 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:kontrol.HttpUtil.java

public static HttpClient getCookielessHttpClient(int timeout) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);

    return httpClient;
}

From source file:com.flipchase.android.network.volley.toolbox.HttpClientStack.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());
    /*UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(Constants.JABOND_SERVER_UID, Constants.JABOND_SERVER_PWD);
            //from   ww w  .j  a  va2s  . c o  m
    BasicScheme scheme = new BasicScheme();
    Header authorizationHeader = null;
    try {
    authorizationHeader = scheme.authenticate(credentials, httpRequest);
    } catch (AuthenticationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    httpRequest.addHeader(authorizationHeader);*/
    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:dk.kk.ibikecphlib.util.HttpUtils.java

public static JsonNode postToServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;//from w w  w .j a va2s  .c o  m
    LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString());
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpPost httppost = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");
        httppost.setHeader("Accept", ACCEPT);
        httppost.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:org.auraframework.integration.test.configuration.JettyTestServletConfig.java

@Override
public HttpClient getHttpClient() {
    /*/*from   w w  w .ja v  a  2  s  . c  o  m*/
     * 10 minute timeout for making a connection and for waiting for data on the connection. This prevents tests
     * from hanging in the http code, which in turn can prevent the server from exiting.
     */
    int timeout = 10 * 60 * 1000;

    CookieStore cookieStore = new BasicCookieStore();

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);

    DefaultHttpClient http = new DefaultHttpClient(params);
    http.setCookieStore(cookieStore);

    return http;
}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.MultiThreadedHttpClient.java

@Override
protected HttpParams createHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, config.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(params, config.getSocketTimeout());
    ConnManagerParams.setMaxConnectionsPerRoute(params,
            new ConnPerRouteBean(config.getMaxConnectionsPerHost()));
    ConnManagerParams.setMaxTotalConnections(params, config.getMaxTotalConnections());
    return params;
}

From source file:jp.co.conit.sss.sp.ex1.util.HttpUtil.java

/**
 * HTTP?????/*from   w w w  . j  a v  a 2s.  c  om*/
 * 
 * @return
 */
private static HttpParams getHttpParam() {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);
    return httpParameters;
}

From source file:com.android.volley.ssl.SslHttpStack.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);/* www .jav a2 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);
    /* Register schemes, HTTP and HTTPS */
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", new PlainSocketFactory(), 80));
    //registry.register(new Scheme("https", new EasySSLSocketFactory(mIsConnectingToYourServer), 443));

    /* Make a thread safe connection manager for the client */
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParams, registry);
    HttpClient httpClient = new DefaultHttpClient(manager, httpParams);

    return httpClient.execute(httpRequest);
}

From source file:com.android.emerson.dl.utils.DownloadHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * //from  ww w .ja v a  2 s . c  om
 * @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, false);

    // 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.fabernovel.alertevoirie.webservice.HttpPostRequest.java

public String sendRequest() throws AVServiceErrorException {
    try {/*from  www.  j a  v  a 2 s.c om*/

        httpPost = new HttpPost(url);

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 20000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

        httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

        httpPost.addHeader(HEADER_APP_VERSION, "1.0.0");
        httpPost.addHeader(HEADER_APP_PLATFORM, "android_family");
        httpPost.addHeader(HEADER_APP_DEVICE_MODEL, (Build.MANUFACTURER + " " + Build.DEVICE).trim());
        httpPost.addHeader(HEADER_APP_REQUEST_SIGNATURE, sha1(MAGIC_KEY + params.get(0).getValue()));

        // Log.i(Constants.PROJECT_TAG,MAGIC_KEY + params.get(0).getValue());

        final HttpResponse response = httpClient.execute(httpPost);
        final HttpEntity entity = response.getEntity();
        content = entity.getContent();
        contentString = convertStreamToString(content);
        Log.d(Constants.PROJECT_TAG, "answer = " + contentString);
    } catch (final UnsupportedEncodingException uee) {
        Log.e(Constants.PROJECT_TAG, "UnsupportedEncodingException", uee);
        throw new AVServiceErrorException(999);
    } catch (final IOException ioe) {
        Log.e(Constants.PROJECT_TAG, "IOException", ioe);
        throw new AVServiceErrorException(999);
    } catch (final IllegalStateException ise) {
        Log.e(Constants.PROJECT_TAG, "IllegalStateException", ise);
        throw new AVServiceErrorException(999);
    } catch (NoSuchAlgorithmException e) {
        Log.e(Constants.PROJECT_TAG, "NoSuchAlgorithmException", e);
        throw new AVServiceErrorException(999);
    } catch (Exception e) {
        Log.e(Constants.PROJECT_TAG, "error in sendRequest : ", e);
        throw new AVServiceErrorException(999);
    }

    try {

        Log.d(Constants.PROJECT_TAG, "contenString: " + contentString);
        JSONObject jo = new JSONObject(contentString);
        int resultnum = jo.getJSONObject(JsonData.PARAM_ANSWER).getInt(JsonData.PARAM_STATUS);
        Log.i(Constants.PROJECT_TAG, "AV Status:" + resultnum);
        if (resultnum != 0)
            throw new AVServiceErrorException(resultnum);

    } catch (JSONException e) {
        Log.w(Constants.PROJECT_TAG, "JSONException in onPostExecute");
        //throw new AVServiceErrorException(999);
    }

    return contentString;
}