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:cl.mmoscoso.geocomm.sync.GeoCommLogInAsyncTask.java

@Override
protected Boolean doInBackground(Void... params) {

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams httpParams = httpclient.getParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
    httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);

    HttpPost httppost = new HttpPost(this.hostname);

    try {//from   w w  w.j a  v  a  2  s.c o  m
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("username", this.name));
        nameValuePairs.add(new BasicNameValuePair("password", this.pass));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();
        //int responseCode =  201;
        switch (responseCode) {
        case 200:
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity);
                //Log.i(TAGNAME, responseBody);
                JSONObject jObj, point;
                try {
                    //Log.i(TAGNAME, "tamao: "+ responseBody);
                    jObj = new JSONObject(responseBody);
                    this.value_login = jObj.getInt("status");
                    this.id_user = jObj.getInt("id");
                    //Log.i(TAGNAME, "Value: "+ this.value_login );
                    //this.name_user = jObj.getString("name");
                    //this.id_user = jObj.getInt("id");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;
        default:
            Log.i(TAGNAME, "Error");
            //Toast.makeText(this.context,"ERROR!!", Toast.LENGTH_SHORT).show();
            break;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return true;
}

From source file:org.zaizi.sensefy.auth.user.acl.ManifoldACLRequester.java

@PostConstruct
public void init() {
    socketTimeOut = 300000;/*from  w w w  .  j  a v a 2s. co  m*/
    poolSize = 50;

    // Initialize the connection pool
    httpConnectionManager = new PoolingClientConnectionManager();
    httpConnectionManager.setMaxTotal(poolSize);
    httpConnectionManager.setDefaultMaxPerRoute(poolSize);
    BasicHttpParams params = new BasicHttpParams();
    params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeOut);
    DefaultHttpClient clientAux = new DefaultHttpClient(httpConnectionManager, params);
    clientAux.setRedirectStrategy(new DefaultRedirectStrategy());
    client = clientAux;
}

From source file:edu.hust.grid.crawl.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());
    params.setBooleanParameter("http.protocol.handle-redirects", false);
    params.setParameter("http.language.Accept-Language", "en-us");
    params.setParameter("http.protocol.content-charset", "UTF-8");
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }// ww  w  .  jav a  2  s  . co  m

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:yin.autoflowcontrol.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

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

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }//from  w  w w .j ava 2  s. c om

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:com.sun.jersey.client.apache4.impl.ReadTimeoutClientTest.java

/**
 * The test defines timeout too 500ms using property and then invokes request
 * which sleeps in the resource method for 1000ms. The timeout should be triggered
 * and exception should be thrown.//from   w  w  w.j  a va  2s .  c  o m
 */
public void testSetTimeoutByHttpClientProperty() {
    startServer(MyResource.class);
    DefaultApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
    config.getProperties().put(CoreConnectionPNames.SO_TIMEOUT, 500);
    ApacheHttpClient4 client = ApacheHttpClient4.create(config);
    try {
        client.resource(getUri().path("resource").build()).get(ClientResponse.class);
        Assert.fail("Should throw the exception.");
    } catch (ClientHandlerException che) {
        Assert.assertEquals(SocketTimeoutException.class, che.getCause().getClass());
    }
}

From source file:org.devtcg.rojocam.rtsp.AbstractRtspServer.java

public AbstractRtspServer() {
    super(TAG);/*  w ww .  ja v a 2 s  . com*/

    /*
     * XXX: Android as a client seems very unhappy if the RTSP connection is
     * closed so we must use an SO_TIMEOUT of infinite to avoid that. The
     * client can close when it wants to.
     */
    mParams = new BasicHttpParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 16384)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, false);

    setDaemon(true);
}

From source file:cl.mmoscoso.geocomm.sync.GeoCommCreatePointAsyncTask.java

@Override
protected Boolean doInBackground(Void... params) {

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams httpParams = httpclient.getParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
    httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);

    HttpPost httppost = new HttpPost(this.hostname);

    //http://172.16.50.35/~ramirez/testAddPoint.php
    //http://172.16.57.132/~laost/Symfony/web/app_dev.php/addPoint/

    try {/*from w w w.  j  ava 2  s . com*/
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("name", this.name));
        nameValuePairs.add(new BasicNameValuePair("description", this.desc));
        nameValuePairs.add(new BasicNameValuePair("latitude", this.latitude));
        nameValuePairs.add(new BasicNameValuePair("longitude", this.longitude));
        nameValuePairs.add(new BasicNameValuePair("route", Integer.toString(this.id_route)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();
        Log.i(TAGNAME, "ERROR: " + responseCode);
        switch (responseCode) {
        default:
            Log.i(TAGNAME, "ERROR");
            //Toast.makeText(this.context,R.string.error_not200code, Toast.LENGTH_SHORT).show();
            break;
        case 200:
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity);
                //Log.i(TAGNAME, responseBody);
                JSONObject jObj, point;
                try {
                    Log.i(TAGNAME, "tamao: " + responseBody);
                    jObj = new JSONObject(responseBody);
                    this.status = jObj.getInt("status");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return true;
}

From source file:com.bbxiaoqu.api.ApiAsyncTask.java

@Override
protected Object doInBackground(Void... params) {
    if (!Utils.isNetworkAvailable(mContext)) {
        return TIMEOUT_ERROR;
    }//  w w w.  java  2 s  . c o m
    String requestUrl = MarketAPI.API_URLS[mReuqestAction];
    requestUrl = GETURL(requestUrl, mParameter);//?GETURL

    HttpEntity requestEntity = null;
    try {
        requestEntity = ApiRequestFactory.getRequestEntity(mReuqestAction, mParameter);
    } catch (UnsupportedEncodingException e) {
        Utils.D("OPPS...This device not support UTF8 encoding.[should not happend]");
        return BUSSINESS_ERROR;
    }
    Object result = null;
    HttpResponse response = null;
    HttpUriRequest request = null;
    try {
        request = ApiRequestFactory.getRequest(requestUrl, mReuqestAction, requestEntity);
        mClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);//
        mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);//
        response = mClient.execute(request);
        final int statusCode = response.getStatusLine().getStatusCode();
        Utils.D("requestUrl " + requestUrl + " statusCode: " + statusCode);
        if (HttpStatus.SC_OK != statusCode) {
            // ?
            return statusCode;
        }
        result = ApiResponseFactory.getResponse(mContext, mReuqestAction, response);
        // ?API Response?BUSSINESS_ERROR?610
        return result == null ? BUSSINESS_ERROR : result;
    } catch (IOException e) {
        Utils.D("Market API encounter the IO exception[mostly is timeout exception]", e);
        return TIMEOUT_ERROR;
    } finally {
        // release the connection
        if (request != null) {
            request.abort();
        }
        if (response != null) {
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    entity.consumeContent();
                }
            } catch (IOException e) {
                Utils.D("release low-level resource error");
            }
        }
    }

}

From source file:org.jclouds.http.apachehc.config.ApacheHCHttpCommandExecutorServiceModule.java

@Singleton
@Provides/*from  www .j  a  va  2 s  . com*/
final HttpParams newBasicHttpParams(HttpUtils utils) {
    BasicHttpParams params = new BasicHttpParams();

    params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "jclouds/1.0");

    if (utils.getConnectionTimeout() > 0) {
        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, utils.getConnectionTimeout());
    }

    if (utils.getSocketOpenTimeout() > 0) {
        params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, utils.getSocketOpenTimeout());
    }

    if (utils.getMaxConnections() > 0)
        ConnManagerParams.setMaxTotalConnections(params, utils.getMaxConnections());

    if (utils.getMaxConnectionsPerHost() > 0) {
        ConnPerRoute connectionsPerRoute = new ConnPerRouteBean(utils.getMaxConnectionsPerHost());
        ConnManagerParams.setMaxConnectionsPerRoute(params, connectionsPerRoute);
    }
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    return params;
}

From source file:cl.mmoscoso.geocomm.sync.GeoCommGetRoutesOwnerAsyncTask.java

@Override
protected Boolean doInBackground(String... params) {
    // TODO Auto-generated method stub

    HttpClient httpclient = new DefaultHttpClient();
    HttpParams httpParams = httpclient.getParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
    httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);

    HttpPost httppost = new HttpPost(this.hostname);

    Log.i(TAGNAME, "Try to connect to " + this.hostname);
    try {/*from  w w w.  j  a  v a  2  s .  c  o m*/
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(this.id_user)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.i(TAGNAME, "Post Request");
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();

        switch (responseCode) {
        default:
            Toast.makeText(this.context, R.string.error_not200code, Toast.LENGTH_SHORT).show();
            break;
        case 200:
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity);
                //Log.i(TAGNAME, responseBody);
                JSONObject jObj, ruta;
                try {
                    Log.i(TAGNAME, "Reading JSONResponse");
                    jObj = new JSONObject(responseBody);
                    SharedPreferences preferences = this.context.getSharedPreferences("userInformation",
                            context.MODE_PRIVATE);
                    Boolean is_allroutes = preferences.getBoolean("is_allroutes", false);
                    for (int i = 0; i < jObj.length(); i++) {
                        ruta = new JSONObject(jObj.getString(jObj.names().getString(i)));
                        if (is_allroutes) {
                            if (ruta.getBoolean("public") == false) {
                                list_routes.add(new GeoCommRoute(ruta.getString("name"),
                                        ruta.getString("owner"), ruta.getInt("id"), ruta.getInt("totalPoints"),
                                        ruta.getString("description"), ruta.getBoolean("public")));
                            }
                        } else
                            list_routes.add(new GeoCommRoute(ruta.getString("name"), ruta.getString("owner"),
                                    ruta.getInt("id"), ruta.getInt("totalPoints"),
                                    ruta.getString("description"), ruta.getBoolean("public")));
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.e(TAGNAME, e.toString());
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(TAGNAME, e.toString());
        return false;
    }
    return true;
}