Search in sources :

Example 6 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class ApiTool method requestPostApi.

public static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    PostMethod method = new PostMethod(apiUrl);
    try {
        String sign = sign(appKey, secret, paramMap);
        paramMap.put("sign", sign);
        paramMap.put("appkey", appKey);
        // 设置HTTP Post数据
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            method.addParameter(entry.getKey(), entry.getValue());
        }
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map)

Example 7 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class DemoApiTool method requestApi.

/**
     * 请求API
     * 
     * @param apiUrl
     * @param appKey
     * @param secret
     * @param paramMap
     * @return
     */
public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    String queryString = getQueryString(appKey, secret, paramMap);
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    HttpMethod method = new GetMethod(apiUrl);
    try {
        if (queryString != null && !queryString.isEmpty()) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            method.setQueryString(encodeQuery);
        }
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (URIException e) {
    } catch (IOException e) {
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : URIException(org.apache.commons.httpclient.URIException) InputStreamReader(java.io.InputStreamReader) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 8 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class DemoApiTool method requestPostApi.

public static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    PostMethod method = new PostMethod(apiUrl);
    try {
        String sign = sign(appKey, secret, paramMap);
        paramMap.put("sign", sign);
        paramMap.put("appkey", appKey);
        // 设置HTTP Post数据
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            method.addParameter(entry.getKey(), entry.getValue());
        }
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map)

Example 9 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project openhab1-addons by openhab.

the class CcuClient method start.

/**
     * {@inheritDoc}
     */
@Override
public void start() throws HomematicClientException {
    logger.info("Starting {}", CcuClient.class.getSimpleName());
    super.start();
    tclregaScripts = loadTclRegaScripts();
    httpClient = new HttpClient(new SimpleHttpConnectionManager(true));
    HttpClientParams params = httpClient.getParams();
    Long timeout = context.getConfig().getTimeout() * 1000L;
    params.setConnectionManagerTimeout(timeout);
    params.setSoTimeout(timeout.intValue());
    params.setContentCharset("ISO-8859-1");
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) SimpleHttpConnectionManager(org.apache.commons.httpclient.SimpleHttpConnectionManager)

Example 10 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project zm-mailbox by Zimbra.

the class ZimbraHttpConnectionManager method createHttpClientParams.

private HttpClientParams createHttpClientParams() {
    HttpClientParams clientParams = new HttpClientParams();
    //
    // Sets the timeout in milliseconds used when retrieving an HTTP connection from the HTTP connection manager. 
    //
    // HttpClientParams.CONNECTION_MANAGER_TIMEOUT
    //
    clientParams.setConnectionManagerTimeout(getParams().getHttpClientConnectionTimeout());
    return clientParams;
}
Also used : HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Aggregations

HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)14 HttpClient (org.apache.commons.httpclient.HttpClient)12 IOException (java.io.IOException)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpMethod (org.apache.commons.httpclient.HttpMethod)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 Map (java.util.Map)3 URIException (org.apache.commons.httpclient.URIException)3 HashMap (java.util.HashMap)2 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)1 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1