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.photon.phresco.nativeapp.eshop.net.HttpRequest.java

/**
 * Send the JSON data to specified URL and get the httpResponse back
 *
 * @param sURL//from   w  ww .  j  a  v  a 2s  . co m
 * @param jObject
 * @return InputStream
 * @throws IOException
 */
public static InputStream post(String sURL, JSONObject jObject) throws IOException {
    HttpResponse httpResponse = null;
    InputStream is = null;

    PhrescoLogger.info(TAG + " post: " + sURL);
    PhrescoLogger.info(TAG + " jObject: " + jObject);

    HttpPost httpPostRequest = new HttpPost(sURL);
    HttpEntity entity;

    httpPostRequest.setHeader("Accept", "application/json");
    httpPostRequest.setHeader(HTTP.CONTENT_TYPE, "application/json");
    httpPostRequest.setEntity(new ByteArrayEntity(jObject.toString().getBytes("UTF-8")));
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = TIME_OUT;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = TIME_OUT;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    httpResponse = httpClient.execute(httpPostRequest);

    if (httpResponse != null) {
        entity = httpResponse.getEntity();
        is = entity.getContent();
    }
    return is;
}

From source file:com.woonoz.proxy.servlet.ProxyServlet.java

public void init(ProxyServletConfig config) {
    targetServer = config.getTargetUrl();
    if (targetServer != null) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme(targetServer.getProtocol(), getPortOrDefault(targetServer.getPort()),
                PlainSocketFactory.getSocketFactory()));
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectionTimeout());
        HttpConnectionParams.setSoTimeout(httpParams, config.getSocketTimeout());
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
        cm.setDefaultMaxPerRoute(config.getMaxConnections());
        cm.setMaxTotal(config.getMaxConnections());
        client = new DefaultHttpClient(cm, httpParams);
        client.removeResponseInterceptorByClass(ResponseProcessCookies.class);
        client.removeRequestInterceptorByClass(RequestAddCookies.class);

        final String remoteUserHeader = config.getRemoteUserHeader();
        if (null != remoteUserHeader) {
            client.addRequestInterceptor(new HttpRequestInterceptor() {

                @Override/*from w  w  w . j a  v  a  2 s .  com*/
                public void process(HttpRequest request, HttpContext context)
                        throws HttpException, IOException {
                    request.removeHeaders(remoteUserHeader);
                    HttpRequestHandler handler;
                    if (context != null && (handler = (HttpRequestHandler) context
                            .getAttribute(HttpRequestHandler.class.getName())) != null) {
                        String remoteUser = handler.getRequest().getRemoteUser();
                        if (remoteUser != null) {
                            request.addHeader(remoteUserHeader, remoteUser);
                        }
                    }
                }
            });
        }
    }
}

From source file:com.scut.easyfe.network.kjFrame.http.HttpClientStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException {
    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);/*w ww.j a va2 s .com*/
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    return mClient.execute(httpRequest);
}

From source file: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());
    onPrepareRequest(httpRequest);/* w  w w .j ava  2  s.com*/
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    return mClient.execute(httpRequest);
}

From source file:com.androidrocks.bex.zxing.client.android.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.
 *///from   w  ww . ja  v  a  2  s  . c o  m
public static AndroidHttpClient 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 20 seconds.  Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 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, false);

    // 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", SSLSocketFactory.getSocketFactory(), 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);
}

From source file:com.nextgis.mobile.map.RemoteTMSLayer.java

public RemoteTMSLayer() {
    super();//from w ww . j  a  v a 2 s  .c o  m

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOKET);

    mHTTPClient = new DefaultHttpClient(httpParameters);
    mHTTPClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, APP_USER_AGENT);
}

From source file:org.frameworkset.spi.remote.http.Client.java

public static HttpParams buildHttpParams() {

    int so_timeout = conparams.getInt("http.socket.timeout", 30);//??

    int CONNECTION_TIMEOUT = conparams.getInt("http.connection.timeout", 30);
    int CONNECTION_Manager_TIMEOUT = conparams.getInt("http.conn-manager.timeout", 2);
    int httpsoLinger = conparams.getInt("http.soLinger", -1);
    HttpParams params = new BasicHttpParams();

    HttpConnectionParams.setSoTimeout(params, so_timeout * 1000);
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT * 1000);
    HttpConnectionParams.setLinger(params, httpsoLinger);
    ConnManagerParams.setTimeout(params, CONNECTION_Manager_TIMEOUT * 1000);
    //      params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, 
    //            "org.frameworkset.spi.remote.http.BBossClientConnectionManagerFactory");
    return params;
}

From source file:com.ibuildapp.romanblack.TableReservationPlugin.utils.TableReservationHTTP.java

/**
 * This method sends HTTP request to add order.
 *
 * @param user//from  w w  w .j ava 2s . com
 * @param handler
 * @param orderInfo
 * @return
 */
public static String sendAddOrderRequest(User user, Handler handler, TableReservationInfo orderInfo) {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
    HttpConnectionParams.setSoTimeout(httpParameters, 15000);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = new HttpPost(ADD_ORDER_URL);
    Log.e(TAG, "ADD_ORDER_URL = " + ADD_ORDER_URL);

    MultipartEntity multipartEntity = new MultipartEntity();

    // user details
    String userType = null;
    String orderUUID = null;
    try {

        if (user.getAccountType() == User.ACCOUNT_TYPES.FACEBOOK) {
            userType = "facebook";
            multipartEntity.addPart("order_customer_name", new StringBody(
                    user.getUserFirstName() + " " + user.getUserLastName(), Charset.forName("UTF-8")));
        } else if (user.getAccountType() == User.ACCOUNT_TYPES.TWITTER) {
            userType = "twitter";
            multipartEntity.addPart("order_customer_name",
                    new StringBody(user.getUserName(), Charset.forName("UTF-8")));
        } else if (user.getAccountType() == User.ACCOUNT_TYPES.IBUILDAPP) {
            userType = "ibuildapp";
            multipartEntity.addPart("order_customer_name",
                    new StringBody(user.getUserName(), Charset.forName("UTF-8")));

        } else if (user.getAccountType() == User.ACCOUNT_TYPES.GUEST) {
            userType = "guest";
            multipartEntity.addPart("order_customer_name", new StringBody("Guest", Charset.forName("UTF-8")));
        }

        multipartEntity.addPart("user_type", new StringBody(userType, Charset.forName("UTF-8")));
        multipartEntity.addPart("user_id", new StringBody(user.getAccountId(), Charset.forName("UTF-8")));
        multipartEntity.addPart("app_id", new StringBody(orderInfo.getAppid(), Charset.forName("UTF-8")));
        multipartEntity.addPart("module_id", new StringBody(orderInfo.getModuleid(), Charset.forName("UTF-8")));

        // order UUID
        orderUUID = UUID.randomUUID().toString();
        multipartEntity.addPart("order_uid", new StringBody(orderUUID, Charset.forName("UTF-8")));

        // order details
        Date tempDate = orderInfo.getOrderDate();
        tempDate.setHours(orderInfo.getOrderTime().houres);
        tempDate.setMinutes(orderInfo.getOrderTime().minutes);
        tempDate.getTimezoneOffset();
        String timeZone = timeZoneToString();

        multipartEntity.addPart("time_zone", new StringBody(timeZone, Charset.forName("UTF-8")));
        multipartEntity.addPart("order_date_time",
                new StringBody(Long.toString(tempDate.getTime() / 1000), Charset.forName("UTF-8")));
        multipartEntity.addPart("order_persons",
                new StringBody(Integer.toString(orderInfo.getPersonsAmount()), Charset.forName("UTF-8")));
        multipartEntity.addPart("order_spec_request",
                new StringBody(orderInfo.getSpecialRequest(), Charset.forName("UTF-8")));
        multipartEntity.addPart("order_customer_phone",
                new StringBody(orderInfo.getPhoneNumber(), Charset.forName("UTF-8")));
        multipartEntity.addPart("order_customer_email",
                new StringBody(orderInfo.getCustomerEmail(), Charset.forName("UTF-8")));

        // add security part
        multipartEntity.addPart("app_id", new StringBody(Statics.appId, Charset.forName("UTF-8")));
        multipartEntity.addPart("token", new StringBody(Statics.appToken, Charset.forName("UTF-8")));

    } catch (Exception e) {
        Log.d("", "");
    }

    httppost.setEntity(multipartEntity);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        String strResponseSaveGoal = httpclient.execute(httppost, responseHandler);
        Log.d("sendAddOrderRequest", "");
        String res = JSONParser.parseQueryError(strResponseSaveGoal);

        if (res == null || res.length() == 0) {
            return orderUUID;
        } else {
            handler.sendEmptyMessage(ADD_REQUEST_ERROR);
            return null;
        }
    } catch (ConnectTimeoutException conEx) {
        handler.sendEmptyMessage(ADD_REQUEST_ERROR);
        return null;
    } catch (ClientProtocolException ex) {
        handler.sendEmptyMessage(ADD_REQUEST_ERROR);
        return null;
    } catch (IOException ex) {
        handler.sendEmptyMessage(ADD_REQUEST_ERROR);
        return null;
    }
}

From source file:com.erdao.PhotSpot.JsonFeedGetter.java

public JsonFeedGetter(Context c, int mode) {
    mode_ = mode;//  w  w  w  . ja v a 2  s .  c  o  m
    context_ = c;
    final HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
    HttpConnectionParams.setConnectionTimeout(httpParams, connection_Timeout);
    HttpConnectionParams.setSoTimeout(httpParams, connection_Timeout);
    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    httpClient_ = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry),
            httpParams);

}

From source file:org.dhis.smssync.net.MainHttpClient.java

public MainHttpClient() {
    httpParameters = new BasicHttpParams();
    httpParameters.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    httpParameters.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));

    httpParameters.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, "utf8");
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    SchemeRegistry schemeRegistry = new SchemeRegistry();

    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);

    httpclient = new DefaultHttpClient(manager, httpParameters);
}