Search in sources :

Example 96 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.

the class SimpleGetRequestExecutor method execute.

@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
    if (queryParam != null) {
        if (uri.indexOf('?') == -1) {
            uri += '?';
        }
        uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
    }
    HttpGet httpGet = new HttpGet(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpGet.setConfig(config);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return responseContent;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 97 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.

the class MaterialNewsInfoRequestExecutor method execute.

public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    Map<String, String> params = new HashMap<>();
    params.put("media_id", materialId);
    httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 98 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.

the class QrCodeRequestExecutor method execute.

@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
    if (ticket != null) {
        if (uri.indexOf('?') == -1) {
            uri += '?';
        }
        uri += uri.endsWith("?") ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
    }
    HttpGet httpGet = new HttpGet(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpGet.setConfig(config);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        Header[] contentTypeHeader = response.getHeaders("Content-Type");
        if (contentTypeHeader != null && contentTypeHeader.length > 0) {
            // 出错
            if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
                String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
                throw new WxErrorException(WxError.fromJson(responseContent));
            }
        }
        InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
        File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
        return localFile;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) Header(org.apache.http.Header) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 99 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project dhis2-core by dhis2.

the class HttpUtils method httpPOST.

/**
 * Method to make an HTTP POST call to a given URL with/without
 * authentication.
 */
public static DhisHttpResponse httpPOST(String requestURL, Object body, boolean authorize, String username, String password, String contentType, int timeout) throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    DhisHttpResponse dhisHttpResponse;
    try {
        HttpPost httpPost = new HttpPost(requestURL);
        httpPost.setConfig(requestConfig);
        if (body instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, String> parameters = (Map<String, String>) body;
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (parameter.getValue() != null) {
                    pairs.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
        } else if (body instanceof String) {
            httpPost.setEntity(new StringEntity((String) body));
        }
        if (!StringUtils.isNotEmpty(contentType)) {
            httpPost.setHeader("Content-Type", contentType);
        }
        if (authorize) {
            httpPost.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpClient.execute(httpPost);
        log.info("Successfully got response from http POST.");
        dhisHttpResponse = processResponse(requestURL, username, response);
    } catch (Exception e) {
        log.error("Exception occurred in httpPOST call with username " + username, e);
        throw e;
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return dhisHttpResponse;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 100 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project dhis2-core by dhis2.

the class HttpUtils method httpGET.

/**
 * Method to make an HTTP GET call to a given URL with/without
 * authentication.
 *
 * @throws Exception
 *         </pre>
 */
public static DhisHttpResponse httpGET(String requestURL, boolean authorize, String username, String password, Map<String, String> headers, int timeout, boolean processResponse) throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
    DhisHttpResponse dhisHttpResponse;
    try {
        HttpGet httpGet = new HttpGet(requestURL);
        httpGet.setConfig(requestConfig);
        if (headers instanceof Map) {
            for (Map.Entry<String, String> e : headers.entrySet()) {
                httpGet.addHeader(e.getKey(), e.getValue());
            }
        }
        if (authorize) {
            httpGet.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpClient.execute(httpGet);
        if (processResponse) {
            dhisHttpResponse = processResponse(requestURL, username, response);
        } else {
            dhisHttpResponse = new DhisHttpResponse(response, null, response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        log.error("Exception occurred in the httpGET call with username " + username, e);
        throw e;
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return dhisHttpResponse;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Map(java.util.Map) IOException(java.io.IOException)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)146 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)97 HttpGet (org.apache.http.client.methods.HttpGet)94 IOException (java.io.IOException)78 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)65 HttpResponse (org.apache.http.HttpResponse)60 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)55 URI (java.net.URI)46 StringEntity (org.apache.http.entity.StringEntity)43 Map (java.util.Map)41 Test (org.junit.Test)41 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 HttpHost (org.apache.http.HttpHost)32 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)32 HttpClient (org.apache.http.client.HttpClient)31 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)27 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)24 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)24