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

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

Introduction

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

Prototype

String CONNECTION_TIMEOUT

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

Click Source Link

Usage

From source file:org.flakor.androidtool.net.net.HistoryTask.java

@Override
protected Integer doInBackground(String... params) {
    String branchId = params[0];/* w w w  . j  a  v a2s .  c  o m*/
    String startTime = params[1];
    String endTime = params[2];

    Debug.error("HistoryTask", "branchid:" + branchId + ";starttime:" + startTime + ";endtime:" + endTime);
    HttpPost httpPost = new HttpPost(HttpThread.SERVER_URL_REMOTE);
    List<NameValuePair> list = new ArrayList<NameValuePair>();

    list.add(new BasicNameValuePair(HttpThread.POST_FUNC, HttpThread.ACTION_HISTORY));
    list.add(new BasicNameValuePair(HttpThread.POST_BID, branchId));
    list.add(new BasicNameValuePair(HttpThread.POST_START_TIME, startTime));
    list.add(new BasicNameValuePair(HttpThread.POST_END_TIME, endTime));

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        HttpClient client = new DefaultHttpClient();
        //
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
        //?
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 8000);
        HttpResponse response = client.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, HTTP.UTF_8);

            Debug.error("HistoryTask", result);
            application = (MyApplication) context.getApplicationContext();
            int resultCode = JsonParser.parseHistory(result, application);

            return resultCode;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return -1;
}

From source file:com.android.wako.net.AsyncHttpPost.java

public boolean process() {
    try {//from w  w  w  .  j  a  va2s . c o m
        //            LogUtil.d(Tag, "---process---url="+url+"?"+getParames());
        request = new HttpPost(url);
        if (Constants.isGzip) {
            request.addHeader("Accept-Encoding", "gzip");
        } else {
            request.addHeader("Accept-Encoding", "default");
        }
        request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        //            LogUtil.d(AsyncHttpPost.class.getName(),
        //                    "AsyncHttpPost  request to url :" + url);

        if (parameter != null && parameter.size() > 0) {
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            for (RequestParameter p : parameter) {
                list.add(new BasicNameValuePair(p.getName(), p.getValue()));
            }

            ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        }
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(2);
            // ??
            byte[] header = new byte[2];
            int result = bis.read(header);
            // reset??
            bis.reset();
            // ?GZIP?
            int headerData = getShort(header);
            // Gzip ? ? 0x1f8b
            if (result != -1 && headerData == 0x1f8b) {
                LogUtil.d("HttpTask", " use GZIPInputStream  ");
                is = new GZIPInputStream(bis);
            } else {
                LogUtil.d("HttpTask", " not use GZIPInputStream");
                is = bis;
            }
            InputStreamReader reader = new InputStreamReader(is, "utf-8");
            char[] data = new char[100];
            int readSize;
            StringBuffer sb = new StringBuffer();
            while ((readSize = reader.read(data)) > 0) {
                sb.append(data, 0, readSize);
            }
            ret = sb.toString();
            bis.close();
            reader.close();
            return true;

        } else {
            mRetStatus = ResStatus.Error_Code;
            RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                    "??,??" + statusCode);
            //                ret = ErrorUtil.errorJson("ERROR.HTTP.001",
            //                        exception.getMessage());
        }

        LogUtil.d(AsyncHttpPost.class.getName(), "AsyncHttpPost  request to url :" + url + "  finished !");
    } catch (IllegalArgumentException e) {
        mRetStatus = ResStatus.Error_IllegalArgument;
        //            RequestException exception = new RequestException(
        //                    RequestException.IO_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.002", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (org.apache.http.conn.ConnectTimeoutException e) {
        mRetStatus = ResStatus.Error_Connect_Timeout;
        //            RequestException exception = new RequestException(
        //                    RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.003", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (java.net.SocketTimeoutException e) {
        mRetStatus = ResStatus.Error_Socket_Timeout;
        //            RequestException exception = new RequestException(
        //                    RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.004", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        mRetStatus = ResStatus.Error_Unsupport_Encoding;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.UNSUPPORTED_ENCODEING_EXCEPTION, "?");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.005", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  UnsupportedEncodingException  " + e.getMessage());
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        mRetStatus = ResStatus.Error_HttpHostConnect;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.CONNECT_EXCEPTION, "");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.006", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  HttpHostConnectException  " + e.getMessage());
    } catch (ClientProtocolException e) {
        mRetStatus = ResStatus.Error_Client_Protocol;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.CLIENT_PROTOL_EXCEPTION, "??");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.007", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        mRetStatus = ResStatus.Error_IOException;
        //            RequestException exception = new RequestException(
        //                    RequestException.IO_EXCEPTION, "??");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.008", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  IOException  " + e.getMessage());
    } catch (Exception e) {
        mRetStatus = ResStatus.Error_IOException;
        e.printStackTrace();
    }
    return false;
}

From source file:uk.co.techblue.alfresco.client.Service.java

/**
 * Gets the client service./* ww w.j a  v  a 2 s  .c  om*/
 * 
 * @param <T> the generic type
 * @param clazz the clazz
 * @param serverUri the server uri
 * @return the client service
 */
private final <T> T getClientService(final Class<T> clazz, final String serverUri) {
    logger.info("Generating REST resource proxy for: " + clazz.getName() + " with socket timeout as: ");
    final HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HTTP_SOCKET_TIMEOUT);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HTTP_CONNECTION_TIMEOUT);
    final ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient);

    return ProxyFactory.create(clazz, serverUri, clientExecutor);
}

From source file:com.nextgis.maplib.datasource.ngw.Connection.java

public HttpClient getHttpClient() {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, APP_USER_AGENT);
    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT_CONNECTION);
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT_SOKET);
    httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);

    return httpclient;
}

From source file:com.couchbase.client.ViewNodeTest.java

private AsyncConnectionManager createConMgr(String host, int port) throws IOReactorException {
    HttpHost target = new HttpHost(host, port);
    int maxConnections = 1;

    HttpParams params = new SyncBasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.USER_AGENT, "Couchbase Java Client 1.1");

    HttpProcessor httpproc = new ImmutableHttpProcessor(
            new HttpRequestInterceptor[] { new RequestContent(), new RequestTargetHost(),
                    new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue(), });

    AsyncNHttpClientHandler protocolHandler = new AsyncNHttpClientHandler(httpproc,
            new ViewNode.MyHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(),
            new DirectByteBufferAllocator(), params);

    protocolHandler.setEventListener(new ViewNode.EventLogger());
    RequeueOpCallback callback = mock(RequeueOpCallback.class);
    AsyncConnectionManager manager = new AsyncConnectionManager(target, maxConnections, protocolHandler, params,
            callback);/*from ww w . java  2  s  .  c  om*/

    return manager;
}

From source file:com.xiaomi.infra.galaxy.sds.client.SdsTHttpClient.java

public void setConnectTimeout(int timeout) {
    connectTimeout_ = timeout;//w w  w .ja v a 2s. co  m
    if (null != this.client) {
        // WARNING, this modifies the HttpClient params, this might have an impact elsewhere if the
        // same HttpClient is used for something else.
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout_);
    }
}

From source file:org.andstatus.app.net.HttpConnectionBasic.java

/**
 * Execute a GET request against the Twitter REST API.
 * //from  ww w.  j  a v  a 2 s . c  o  m
 * @param url
 * @return String
 * @throws ConnectionException
 */
@Override
public JSONTokener getRequest(HttpGet getMethod) throws ConnectionException {
    JSONTokener jso = null;
    String response = null;
    boolean ok = false;
    int statusCode = 0;
    HttpClient client = HttpApacheUtils.getHttpClient();
    try {
        getMethod.setHeader("User-Agent", HttpConnection.USER_AGENT);
        getMethod.addHeader("Authorization", "Basic " + getCredentials());
        client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                MyPreferences.getConnectionTimeoutMs());
        client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                MyPreferences.getConnectionTimeoutMs());
        HttpResponse httpResponse = client.execute(getMethod);
        statusCode = httpResponse.getStatusLine().getStatusCode();
        response = retrieveInputStream(httpResponse.getEntity());
        jso = new JSONTokener(response);
        ok = true;
    } catch (Exception e) {
        MyLog.e(this, "getRequest", e);
        throw new ConnectionException(e);
    } finally {
        getMethod.abort();
    }
    parseStatusCode(statusCode);
    if (!ok) {
        jso = null;
    }
    return jso;
}

From source file:com.couchbase.client.ViewConnection.java

/**
 * Create ViewNode connections and queue them up for connect.
 *
 * This method also defines the connection params for each connection,
 * including the default settings like timeouts and the user agent string.
 *
 * @param addrs addresses of all the nodes it should connect to.
 * @return Returns a list of the ViewNodes.
 * @throws IOException/*from ww w  .j av a  2s  . c om*/
 */
private List<ViewNode> createConnections(List<InetSocketAddress> addrs) throws IOException {

    List<ViewNode> nodeList = new LinkedList<ViewNode>();

    for (InetSocketAddress a : addrs) {
        HttpParams params = new SyncBasicHttpParams();
        params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
                .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000)
                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
                .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
                .setParameter(CoreProtocolPNames.USER_AGENT, "Couchbase Java Client 1.0.2");

        HttpProcessor httpproc = new ImmutableHttpProcessor(
                new HttpRequestInterceptor[] { new RequestContent(), new RequestTargetHost(),
                        new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue(), });

        AsyncNHttpClientHandler protocolHandler = new AsyncNHttpClientHandler(httpproc,
                new MyHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(),
                new DirectByteBufferAllocator(), params);
        protocolHandler.setEventListener(new EventLogger());

        AsyncConnectionManager connMgr = new AsyncConnectionManager(new HttpHost(a.getHostName(), a.getPort()),
                NUM_CONNS, protocolHandler, params, new RequeueOpCallback(this));
        getLogger().info("Added %s to connect queue", a.getHostName());

        ViewNode node = connFactory.createViewNode(a, connMgr);
        node.init();
        nodeList.add(node);
    }

    return nodeList;
}