Search in sources :

Example 56 with RequestConfig

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

the class WxMpServiceImpl method getPrepayId.

public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
    String nonce_str = System.currentTimeMillis() + "";
    final SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("appid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    checkParameters(packageParams);
    String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
    packageParams.put("sign", sign);
    StringBuilder request = new StringBuilder("<xml>");
    for (Entry<String, String> para : packageParams.entrySet()) {
        request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
    }
    request.append("</xml>");
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder");
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
    httpPost.setEntity(entity);
    try {
        CloseableHttpResponse response = getHttpclient().execute(httpPost);
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        XStream xstream = XStreamInitializer.getInstance();
        xstream.alias("xml", WxMpPrepayIdResult.class);
        WxMpPrepayIdResult wxMpPrepayIdResult = (WxMpPrepayIdResult) xstream.fromXML(responseContent);
        return wxMpPrepayIdResult;
    } catch (IOException e) {
        throw new RuntimeException("Failed to get prepay id due to IO exception.", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxMpPrepayIdResult(me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 57 with RequestConfig

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

the class WxMpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxMpConfigStorage.expireAccessToken();
    }
    if (wxMpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxMpConfigStorage.isAccessTokenExpired()) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + wxMpConfigStorage.getAppId() + "&secret=" + wxMpConfigStorage.getSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpResponse response = getHttpclient().execute(httpGet);
                    String resultContent = new BasicResponseHandler().handleResponse(response);
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxMpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) 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 58 with RequestConfig

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

the class MaterialUploadRequestExecutor method execute.

public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(response);
    }
    if (material != null) {
        File file = material.getFile();
        if (file == null || !file.exists()) {
            throw new FileNotFoundException();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName())).setMode(HttpMultipartMode.RFC6532);
        Map<String, String> form = material.getForm();
        if (material.getForm() != null) {
            multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
        }
        httpPost.setEntity(multipartEntityBuilder.build());
        httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }
    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 WxMpMaterialUploadResult.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) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

Example 59 with RequestConfig

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

the class MaterialVideoInfoRequestExecutor method execute.

public WxMpMaterialVideoInfoResult 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 WxMpMaterialVideoInfoResult.fromJson(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) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 60 with RequestConfig

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

the class MaterialVoiceAndImageDownloadRequestExecutor method execute.

public InputStream 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);
    // 下载媒体文件出错
    InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
    byte[] responseContent = IOUtils.toByteArray(inputStream);
    String responseContentString = new String(responseContent, "UTF-8");
    if (responseContentString.length() < 100) {
        try {
            WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
            if (wxError.getErrorCode() != 0) {
                throw new WxErrorException(wxError);
            }
        } catch (com.google.gson.JsonSyntaxException ex) {
            return new ByteArrayInputStream(responseContent);
        }
    }
    return new ByteArrayInputStream(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) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) StringEntity(org.apache.http.entity.StringEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

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