Search in sources :

Example 46 with RequestConfig

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

the class MediaDownloadRequestExecutor method execute.

@Override
public File 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)) {
        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);
        // 视频文件不支持下载
        String fileName = getFileName(response);
        if (StringUtils.isBlank(fileName)) {
            return null;
        }
        String[] name_ext = fileName.split("\\.");
        File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], tmpDirFile);
        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 47 with RequestConfig

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

the class MediaUploadRequestExecutor method execute.

@Override
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    if (file != null) {
        HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("media", file).setMode(HttpMultipartMode.RFC6532).build();
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }
    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return WxMediaUploadResult.fromJson(responseContent);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) HttpEntity(org.apache.http.HttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 48 with RequestConfig

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

the class SimplePostRequestExecutor method execute.

@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    if (postEntity != null) {
        StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
        httpPost.setEntity(entity);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return responseContent;
    }
}
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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 49 with RequestConfig

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

the class WxCpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxCpConfigStorage.expireAccessToken();
    }
    if (wxCpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxCpConfigStorage.isAccessTokenExpired()) {
                String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" + "&corpid=" + wxCpConfigStorage.getCorpId() + "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpClient httpclient = getHttpclient();
                    String resultContent = null;
                    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                        resultContent = new BasicResponseHandler().handleResponse(response);
                    }
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxCpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxCpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) WxError(me.chanjar.weixin.common.bean.result.WxError) WxAccessToken(me.chanjar.weixin.common.bean.WxAccessToken) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 50 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project dropwizard by dropwizard.

the class DropwizardApacheConnector method buildApacheRequest.

/**
     * Build a new Apache's {@link org.apache.http.client.methods.HttpUriRequest}
     * from Jersey's {@link org.glassfish.jersey.client.ClientRequest}
     * <p>
     * Convert a method, URI, body, headers and override a user-agent if necessary
     * </p>
     *
     * @param jerseyRequest representation of an HTTP request in Jersey
     * @return a new {@link org.apache.http.client.methods.HttpUriRequest}
     */
private HttpUriRequest buildApacheRequest(ClientRequest jerseyRequest) {
    final RequestBuilder builder = RequestBuilder.create(jerseyRequest.getMethod()).setUri(jerseyRequest.getUri()).setEntity(getHttpEntity(jerseyRequest));
    for (String headerName : jerseyRequest.getHeaders().keySet()) {
        builder.addHeader(headerName, jerseyRequest.getHeaderString(headerName));
    }
    final Optional<RequestConfig> requestConfig = addJerseyRequestConfig(jerseyRequest);
    requestConfig.ifPresent(builder::setConfig);
    return builder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6