mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-29 01:18:36 +08:00
okhttp用法有错误;添加了枚举HttpType (#207)
This commit is contained in:
@ -17,26 +17,29 @@ import okhttp3.ConnectionPool;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/4/28.
|
||||
*/
|
||||
public enum HttpType {
|
||||
joddHttp, apacheHttp, okHttp;
|
||||
}
|
||||
@ -213,7 +213,7 @@ public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File,
|
||||
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);
|
||||
|
||||
@ -136,7 +136,7 @@ public class MediaUploadRequestExecutor extends AbstractRequestExecutor<WxMediaU
|
||||
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);
|
||||
|
||||
@ -17,4 +17,10 @@ public interface RequestHttp<H,P> {
|
||||
*/
|
||||
P getRequestHttpProxy();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
HttpType getRequestType();
|
||||
|
||||
}
|
||||
|
||||
@ -141,7 +141,7 @@ public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, St
|
||||
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);
|
||||
|
||||
@ -156,7 +156,7 @@ public class SimplePostRequestExecutor extends AbstractRequestExecutor<String, S
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user