diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AbstractRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AbstractRequestExecutor.java index 23881e6635..731bec777e 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AbstractRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AbstractRequestExecutor.java @@ -17,26 +17,29 @@ public abstract class AbstractRequestExecutor implements RequestExecutor { @Override - public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException{ - if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { - //apache-http请求 - CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); - HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); - return executeApache(httpClient, httpProxy, uri, data); - } - if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { - //jodd-http请求 - HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); - ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); - return executeJodd(provider, proxyInfo, uri, data); - } else if (requestHttp.getRequestHttpClient() instanceof ConnectionPool) { - //okhttp请求 - ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient(); - OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy(); - return executeOkhttp(pool, proxyInfo, uri, data); - } else { - //TODO 这里需要抛出异常,需要优化 - return null; + public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException { + switch (requestHttp.getRequestType()) { + case apacheHttp: { + //apache-http请求 + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, data); + } + case joddHttp: { + //jodd-http请求 + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, data); + } + case okHttp: { + //okhttp请求 + ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient(); + OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy(); + return executeOkhttp(pool, proxyInfo, uri, data); + } + default: + //TODO 这里需要抛出异常,需要优化 + return null; } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpType.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpType.java new file mode 100644 index 0000000000..5e78a6f1d0 --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpType.java @@ -0,0 +1,8 @@ +package me.chanjar.weixin.common.util.http; + +/** + * Created by ecoolper on 2017/4/28. + */ +public enum HttpType { + joddHttp, apacheHttp, okHttp; +} 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 e95f02de24..6da7c65c6c 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 @@ -213,7 +213,7 @@ public Request authenticate(Route route, Response response) throws IOException { String contentType = response.header("Content-Type"); if (contentType != null && contentType.startsWith("application/json")) { // application/json; encoding=utf-8 下载媒体文件出错 - throw new WxErrorException(WxError.fromJson(response.body().toString())); + throw new WxErrorException(WxError.fromJson(response.body().string())); } String fileName = getFileName(response); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java index 42040834ab..a6366f546f 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java @@ -136,7 +136,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(uri).post(body).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestHttp.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestHttp.java index 5af68e0710..13eaf93989 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestHttp.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestHttp.java @@ -17,4 +17,10 @@ public interface RequestHttp { */ P getRequestHttpProxy(); + /** + * + * @return + */ + HttpType getRequestType(); + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java index 5edb70f3a0..f29739d675 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java @@ -141,7 +141,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(uri).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); 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 5517ab636b..c89309b7bd 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 @@ -156,7 +156,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(uri).post(body).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java index 92579d4f2a..61b75cb5d5 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.cp.api.WxCpConfigStorage; @@ -32,6 +33,11 @@ public HttpHost getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.apacheHttp; + } + @Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { if (forceRefresh) { diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/jodd/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/jodd/WxCpServiceImpl.java index 2096ffd180..1dc2dd0efc 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/jodd/WxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/jodd/WxCpServiceImpl.java @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.cp.api.WxCpConfigStorage; import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl; @@ -22,6 +23,11 @@ public ProxyInfo getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.joddHttp; + } + @Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { if (forceRefresh) { diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/okhttp/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/okhttp/WxCpServiceImpl.java index c33015d73b..ae4eab2c35 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/okhttp/WxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/okhttp/WxCpServiceImpl.java @@ -5,6 +5,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; import me.chanjar.weixin.cp.api.WxCpConfigStorage; import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl; @@ -25,6 +26,11 @@ public OkhttpProxyInfo getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.okHttp; + } + @Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { if (forceRefresh) { @@ -62,7 +68,12 @@ public Request authenticate(Route route, Response response) throws IOException { } catch (IOException e) { e.printStackTrace(); } - String resultContent = response.body().toString(); + String resultContent = null; + try { + resultContent = response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + } WxError error = WxError.fromJson(resultContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java index 3032f59df9..835fbe1897 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java @@ -1,13 +1,24 @@ package me.chanjar.weixin.mp.api.impl.apache; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import me.chanjar.weixin.common.bean.WxAccessToken; +import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.session.StandardSessionManager; +import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.common.util.RandomUtils; +import me.chanjar.weixin.common.util.crypto.SHA1; +import me.chanjar.weixin.common.util.http.*; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; -import me.chanjar.weixin.mp.api.WxMpConfigStorage; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl; +import me.chanjar.weixin.mp.api.*; +import me.chanjar.weixin.mp.api.impl.*; +import me.chanjar.weixin.mp.bean.*; +import me.chanjar.weixin.mp.bean.result.*; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; @@ -35,6 +46,11 @@ public HttpHost getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.apacheHttp; + } + @Override public void initHttp() { WxMpConfigStorage configStorage = this.getWxMpConfigStorage(); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java index 23f88b14ac..677e2e6b6d 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java @@ -5,6 +5,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.mp.api.*; import me.chanjar.weixin.mp.api.impl.*; @@ -27,6 +28,11 @@ public ProxyInfo getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.joddHttp; + } + @Override public void initHttp() { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/okhttp/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/okhttp/WxMpServiceImpl.java index a8e0aeb99e..dc6d7d0051 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/okhttp/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/okhttp/WxMpServiceImpl.java @@ -3,6 +3,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo; import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; @@ -26,6 +27,11 @@ public OkhttpProxyInfo getRequestHttpProxy() { return httpProxy; } + @Override + public HttpType getRequestType() { + return HttpType.okHttp; + } + @Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); @@ -60,7 +66,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(url).get().build(); Response response = client.newCall(request).execute(); - String resultContent = response.body().toString(); + String resultContent = response.body().string(); WxError error = WxError.fromJson(resultContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java index 15e636bb6c..d6c68bf963 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java @@ -102,7 +102,7 @@ public Request authenticate(Route route, Response response) throws IOException { RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); Request request = new Request.Builder().url(uri).post(requestBody).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java index 58e63a92b0..de5f23429c 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java @@ -104,7 +104,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(uri).post(requestBody).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java index a23af2b544..772684ab7c 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java @@ -104,7 +104,7 @@ public Request authenticate(Route route, Response response) throws IOException { RequestBody body =bodyBuilder.build(); Request request = new Request.Builder().url(uri).post(body).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java index a6af76eb14..3c481f7ca1 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java @@ -77,7 +77,7 @@ public Request authenticate(Route route, Response response) throws IOException { RequestBody requestBody =new FormBody.Builder().add("media_id", materialId).build(); Request request = new Request.Builder().url(uri).post(requestBody).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java index b0db70dc9e..4232f33316 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java @@ -90,7 +90,7 @@ public Request authenticate(Route route, Response response) throws IOException { Request request = new Request.Builder().url(uri).post(body).build(); Response response = client.newCall(request).execute(); - String responseContent = response.body().toString(); + String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java index 91e38eb53b..d37d0bb96a 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java @@ -92,7 +92,7 @@ public Request authenticate(Route route, Response response) throws IOException { Response response = client.newCall(request).execute(); String contentTypeHeader = response.header("Content-Type"); if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { - String responseContent = response.body().toString(); + String responseContent = response.body().string(); throw new WxErrorException(WxError.fromJson(responseContent)); } try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {