From d3d61d753e251d088d5d941b9bc9e2834aed82b8 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Mon, 13 Jul 2015 17:25:46 +0800 Subject: [PATCH] #165 #167 --- .../http/MediaDownloadRequestExecutor.java | 26 ++++++++----------- .../util/http/MediaUploadRequestExecutor.java | 13 +++++----- .../util/http/SimpleGetRequestExecutor.java | 13 +++++----- .../util/http/SimplePostRequestExecutor.java | 13 +++++----- .../weixin/cp/api/WxCpServiceImpl.java | 6 +++-- .../mp/util/http/QrCodeRequestExecutor.java | 25 +++++++++--------- 6 files changed, 49 insertions(+), 47 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java index fff109bb..f9e3a25f 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java @@ -53,19 +53,17 @@ public class MediaDownloadRequestExecutor implements RequestExecutor 0) { - // 下载媒体文件出错 - if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - throw new WxErrorException(WxError.fromJson(responseContent)); + 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 = null; - try { - inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); // 视频文件不支持下载 String fileName = getFileName(response); @@ -75,11 +73,9 @@ public class MediaDownloadRequestExecutor implements RequestExecutor httpGet.setConfig(config); } - CloseableHttpResponse response = httpclient.execute(httpGet); - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); + 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; } - return responseContent; } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java index d50fe251..1ab1ad7e 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java @@ -40,13 +40,14 @@ public class SimplePostRequestExecutor implements RequestExecutor 0) { - // 出错 - if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - throw new WxErrorException(WxError.fromJson(responseContent)); + 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; } - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); - - File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); - return localFile; + } }