Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@
public abstract class AbstractRequestExecutor<T, E> implements RequestExecutor<T, E> {

@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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.chanjar.weixin.common.util.http;

/**
* Created by ecoolper on 2017/4/28.
*/
public enum HttpType {
joddHttp, apacheHttp, okHttp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public interface RequestHttp<H,P> {
*/
P getRequestHttpProxy();

/**
*
* @return
*/
HttpType getRequestType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,6 +46,11 @@ public HttpHost getRequestHttpProxy() {
return httpProxy;
}

@Override
public HttpType getRequestType() {
return HttpType.apacheHttp;
}

@Override
public void initHttp() {
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -27,6 +28,11 @@ public ProxyInfo getRequestHttpProxy() {
return httpProxy;
}

@Override
public HttpType getRequestType() {
return HttpType.joddHttp;
}

@Override
public void initHttp() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down