Search in sources :

Example 21 with WxError

use of me.chanjar.weixin.common.bean.result.WxError 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 22 with WxError

use of me.chanjar.weixin.common.bean.result.WxError 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)

Example 23 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method executeInternal.

protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    if (uri.indexOf("access_token=") != -1) {
        throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
    }
    String accessToken = getAccessToken(false);
    String uriWithAccessToken = uri;
    uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
    try {
        return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
    } catch (WxErrorException e) {
        WxError error = e.getError();
        /*
       * 发生以下情况时尝试刷新access_token
       * 40001 获取access_token时AppSecret错误,或者access_token无效
       * 42001 access_token超时
       */
        if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
            // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
            wxMpConfigStorage.expireAccessToken();
            return execute(executor, uri, data);
        }
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return null;
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

WxError (me.chanjar.weixin.common.bean.result.WxError)23 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)19 RequestConfig (org.apache.http.client.config.RequestConfig)11 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 HttpPost (org.apache.http.client.methods.HttpPost)8 HashMap (java.util.HashMap)6 StringEntity (org.apache.http.entity.StringEntity)6 IOException (java.io.IOException)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 JsonObject (com.google.gson.JsonObject)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 XStream (com.thoughtworks.xstream.XStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 TreeMap (java.util.TreeMap)1 WxRedpackResult (me.chanjar.weixin.mp.bean.result.WxRedpackResult)1