Search in sources :

Example 51 with URIException

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

the class DemoApiTool method requestApi.

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 (StringUtils.isNotBlank(queryString)) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            LOGGER.debug("Encoded Query:" + encodeQuery);
            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) {
        LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", 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 52 with URIException

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

the class ApiTool method requestApi.

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 (StringUtils.isNotBlank(queryString)) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            LOGGER.debug("Encoded Query:" + encodeQuery);
            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) {
        LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", 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)

Aggregations

URIException (org.apache.commons.httpclient.URIException)52 URI (org.apache.commons.httpclient.URI)31 IOException (java.io.IOException)9 HttpMethod (org.apache.commons.httpclient.HttpMethod)8 Header (org.apache.commons.httpclient.Header)7 HttpClient (org.apache.commons.httpclient.HttpClient)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 DatabaseException (org.parosproxy.paros.db.DatabaseException)5 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpException (org.apache.commons.httpclient.HttpException)3 HttpMessage (org.parosproxy.paros.network.HttpMessage)3 InvalidParameterException (java.security.InvalidParameterException)2 HashMap (java.util.HashMap)2