Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Loading