Example usage for org.apache.http.client HttpClient getParams

List of usage examples for org.apache.http.client HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getParams.

Prototype

@Deprecated
HttpParams getParams();

Source Link

Document

Obtains the parameters for this client.

Usage

From source file:com.traffic.common.utils.http.HttpClientUtils.java

/**
 * ,result""/*from   w w w  .j a  v  a2 s .c om*/
 * 
 * @param url
 * @param param
 * @return
 */
public static String httpGet(String url) {
    logger.info("httpGet URL [" + url + "] start ");
    HttpClient httpclient = null;
    HttpGet httpGet = null;
    HttpResponse response = null;
    HttpEntity entity = null;
    String result = "";
    try {
        httpclient = HttpConnectionManager.getHttpClient();
        // cookie---??
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpGet = new HttpGet(url);
        // ???
        for (Entry<String, String> entry : headers.entrySet()) {
            httpGet.setHeader(entry.getKey(), entry.getValue());
        }
        response = httpclient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("HttpStatus ERROR" + "Method failed: " + response.getStatusLine());
            return "";
        } else {
            entity = response.getEntity();
            if (null != entity) {
                byte[] bytes = EntityUtils.toByteArray(entity);
                result = new String(bytes, "UTF-8");
            } else {
                logger.error("httpGet URL [" + url + "],httpEntity is null.");
            }
            return result;
        }
    } catch (Exception e) {
        logger.error("httpGet URL [" + url + "] error, ", e);
        return "";
    }
}

From source file:com.upyun.sdk.utils.HttpClientUtils.java

private static HttpResponse head(String url, HttpClient httpclient, Map<String, String> headers) {
    HttpHead httpHead = new HttpHead(url);
    HttpResponse response = null;//  ww  w  .j  a va2s .  c  o m
    if (headers != null) {
        for (String key : headers.keySet()) {
            httpHead.addHeader(key, headers.get(key));
        }
    }

    httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 
    httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, SO_TIMEOUT); // ?

    try {
        response = httpclient.execute(httpHead);
    } catch (Exception e) {
        LogUtil.exception(logger, e);
    }

    return response;
}

From source file:test.Http.java

/**
 * post url :??jsonString???json,?json/*  www .j  a  v a  2  s.c  om*/
 * 
 * @date
 * @param url
 * @param jsonString
 * @return
 * @throws Exception
 */
public static String httpPostWithJsons(String url, String jsonString) throws Exception {
    System.out.println("==url++" + url + "==jsonString" + jsonString);
    String responseBodyData = null;
    HttpClient httpClient = new DefaultHttpClient();
    // 
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
    // ?
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "text/html");
    // 
    byte[] requestStrings = jsonString.toString().getBytes("UTF-8");
    ByteArrayEntity byteArrayEntity = new ByteArrayEntity(requestStrings);
    httpPost.setEntity(byteArrayEntity);

    try {
        HttpResponse response = httpClient.execute(httpPost);// post
        if (response.getStatusLine().getStatusCode() == 200) {
            responseBodyData = EntityUtils.toString(response.getEntity());
        } else {
            responseBodyData = null;
        }
    } catch (Exception e) {
    } finally {
        httpPost.abort();
        httpClient = null;
    }
    return responseBodyData;
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * HTTP DELETE/*from w  ww.  j  a  v a 2  s .co  m*/
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpDelete(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, List<String> signHeaderPrefixList, String appKey, String appSecret)
        throws Exception {
    headers = initialBasicHeader(HttpMethod.DELETE, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpDelete delete = new HttpDelete(initUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        delete.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    return convert(httpClient.execute(delete));
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * HTTP GET//  ww  w .  j  a v a 2s.  c o m
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpGet(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, List<String> signHeaderPrefixList, String appKey, String appSecret)
        throws Exception {
    headers = initialBasicHeader(HttpMethod.GET, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpGet get = new HttpGet(initUrl(host, path, querys));

    for (Map.Entry<String, String> e : headers.entrySet()) {
        get.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    return convert(httpClient.execute(get));
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * HTTP POST /*from  ww w. j av  a 2 s.  c  om*/
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param bodys
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpPost(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, byte[] bodys, List<String> signHeaderPrefixList, String appKey,
        String appSecret) throws Exception {
    headers = initialBasicHeader(HttpMethod.POST, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpPost post = new HttpPost(initUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        post.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    if (bodys != null) {
        post.setEntity(new ByteArrayEntity(bodys));
    }

    return convert(httpClient.execute(post));
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * HTTP PUT//  w ww  .j a v a  2s  . c om
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param bodys
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpPut(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, byte[] bodys, List<String> signHeaderPrefixList, String appKey,
        String appSecret) throws Exception {
    headers = initialBasicHeader(HttpMethod.PUT, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpPut put = new HttpPut(initUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        put.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    if (bodys != null) {
        put.setEntity(new ByteArrayEntity(bodys));
    }

    return convert(httpClient.execute(put));
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * Http POST /*  w ww. j a  v  a2 s  .co  m*/
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param body
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpPost(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, String body, List<String> signHeaderPrefixList, String appKey,
        String appSecret) throws Exception {
    headers = initialBasicHeader(HttpMethod.POST, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpPost post = new HttpPost(initUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        post.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    if (StringUtils.isNotBlank(body)) {
        post.setEntity(new StringEntity(body, Constants.ENCODING));

    }

    return convert(httpClient.execute(post));
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

/**
 * HTTP PUT //from   w w w.j av a2s  .c o m
 * @param host
 * @param path
 * @param connectTimeout
 * @param headers
 * @param querys
 * @param body
 * @param signHeaderPrefixList
 * @param appKey
 * @param appSecret
 * @return
 * @throws Exception
 */
public static Response httpPut(String host, String path, int connectTimeout, Map<String, String> headers,
        Map<String, String> querys, String body, List<String> signHeaderPrefixList, String appKey,
        String appSecret) throws Exception {
    headers = initialBasicHeader(HttpMethod.PUT, path, headers, querys, null, signHeaderPrefixList, appKey,
            appSecret);

    HttpClient httpClient = wrapClient(host);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout));

    HttpPut put = new HttpPut(initUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        put.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
    }

    if (StringUtils.isNotBlank(body)) {
        put.setEntity(new StringEntity(body, Constants.ENCODING));

    }

    return convert(httpClient.execute(put));
}

From source file:com.traffic.common.utils.http.HttpClientUtils.java

/**
 * ,result""//from www. ja v  a2s  .  c  o m
 * 
 * @param url
 * @param param
 * @return
 */
public static String httpPost(String url, String paramvalue) {
    logger.info("httpPost URL [" + url + "] start ");
    if (logger.isDebugEnabled()) {
        logger.debug("httpPost body :" + paramvalue);
    }
    logger.info("httpPost body :" + paramvalue);
    HttpClient httpclient = null;
    HttpPost httpPost = null;
    HttpResponse response = null;
    HttpEntity entity = null;
    String result = "";

    try {
        httpclient = HttpConnectionManager.getHttpClient();
        // cookie---??
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpPost = new HttpPost(url);
        // ???
        for (Entry<String, String> entry : headers.entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }

        StringEntity stringEntity = new StringEntity(paramvalue, "UTF-8");
        httpPost.setEntity(stringEntity);

        // 
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 40000);
        // ?
        HttpConnectionParams.setSoTimeout(httpPost.getParams(), 200000);
        response = httpclient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("HttpStatus ERROR" + "Method failed: " + response.getStatusLine());
            return "";
        } else {
            entity = response.getEntity();
            if (null != entity) {
                byte[] bytes = EntityUtils.toByteArray(entity);
                result = new String(bytes, "UTF-8");
            } else {
                logger.error("httpPost URL [" + url + "],httpEntity is null.");
            }
            return result;
        }
    } catch (Exception e) {
        logger.error("httpPost URL [" + url + "] error, ", e);
        return "";
    }
}