mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	#293 重构OkHttp的实现代码,同时修复JSApi的bug
* update travis settings * feat(okhttp): 修改okhttp底层调用方法 直接用OkHttpClient代替connect.使客户端单一化.Okhttp 自动管理连接池优化 * feat(log,jsApi): 添加log debug 标记明确下调用底层效果,修复jsAPI Lock 为null 问题 添加log debug 标记明确下调用底层效果,修复jsAPI Lock 为null 问题 #293
This commit is contained in:
		| @ -6,7 +6,11 @@ import me.chanjar.weixin.common.util.fs.FileUtils; | ||||
| import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | ||||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||||
| import okhttp3.*; | ||||
| import okio.BufferedSink; | ||||
| import okio.Okio; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.File; | ||||
| @ -18,7 +22,8 @@ import java.util.regex.Pattern; | ||||
| /** | ||||
|  * Created by ecoolper on 2017/5/5. | ||||
|  */ | ||||
| public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<ConnectionPool, OkHttpProxyInfo> { | ||||
| public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||||
|   private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
|  | ||||
|  | ||||
|   public OkHttpMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | ||||
| @ -27,6 +32,7 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec | ||||
|  | ||||
|   @Override | ||||
|   public File execute(String uri, String queryParam) throws WxErrorException, IOException { | ||||
|     logger.debug("OkHttpMediaDownloadRequestExecutor is running"); | ||||
|     if (queryParam != null) { | ||||
|       if (uri.indexOf('?') == -1) { | ||||
|         uri += '?'; | ||||
| @ -34,23 +40,8 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec | ||||
|       uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | ||||
|     } | ||||
|  | ||||
|     OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | ||||
|     //设置代理 | ||||
|     if (requestHttp.getRequestHttpProxy() != null) { | ||||
|       clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | ||||
|     } | ||||
|     //设置授权 | ||||
|     clientBuilder.authenticator(new Authenticator() { | ||||
|       @Override | ||||
|       public Request authenticate(Route route, Response response) throws IOException { | ||||
|         String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | ||||
|         return response.request().newBuilder() | ||||
|           .header("Authorization", credential) | ||||
|           .build(); | ||||
|       } | ||||
|     }); | ||||
|     //得到httpClient | ||||
|     OkHttpClient client = clientBuilder.build(); | ||||
|     OkHttpClient client = requestHttp.getRequestHttpClient(); | ||||
|  | ||||
|     Request request = new Request.Builder().url(uri).get().build(); | ||||
|  | ||||
| @ -66,10 +57,12 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec | ||||
|     if (StringUtils.isBlank(fileName)) { | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     InputStream inputStream = new ByteArrayInputStream(response.body().bytes()); | ||||
|     String[] nameAndExt = fileName.split("\\."); | ||||
|     return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile); | ||||
|     File file = File.createTempFile(nameAndExt[0], nameAndExt[1], super.tmpDirFile); | ||||
|     try (BufferedSink sink = Okio.buffer(Okio.sink(file))) { | ||||
|       sink.writeAll(response.body().source()); | ||||
|     } | ||||
|     return file; | ||||
|   } | ||||
|  | ||||
|   private String getFileName(Response response) throws WxErrorException { | ||||
|  | ||||
| @ -6,6 +6,8 @@ import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | ||||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||||
| import okhttp3.*; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| @ -13,7 +15,8 @@ import java.io.IOException; | ||||
| /** | ||||
|  * Created by ecoolper on 2017/5/5. | ||||
|  */ | ||||
| public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<ConnectionPool, OkHttpProxyInfo> { | ||||
| public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||||
|   private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
|  | ||||
|   public OkHttpMediaUploadRequestExecutor(RequestHttp requestHttp) { | ||||
|     super(requestHttp); | ||||
| @ -21,23 +24,9 @@ public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor | ||||
|  | ||||
|   @Override | ||||
|   public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { | ||||
|     OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | ||||
|     //设置代理 | ||||
|     if (requestHttp.getRequestHttpProxy() != null) { | ||||
|       clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | ||||
|     } | ||||
|     //设置授权 | ||||
|     clientBuilder.authenticator(new Authenticator() { | ||||
|       @Override | ||||
|       public Request authenticate(Route route, Response response) throws IOException { | ||||
|         String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | ||||
|         return response.request().newBuilder() | ||||
|           .header("Authorization", credential) | ||||
|           .build(); | ||||
|       } | ||||
|     }); | ||||
|     logger.debug("OkHttpMediaUploadRequestExecutor is running"); | ||||
|     //得到httpClient | ||||
|     OkHttpClient client = clientBuilder.build(); | ||||
|     OkHttpClient client = requestHttp.getRequestHttpClient(); | ||||
|  | ||||
|     RequestBody body = new MultipartBody.Builder() | ||||
|       .setType(MediaType.parse("multipart/form-data")) | ||||
|  | ||||
| @ -5,13 +5,16 @@ import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | ||||
| import okhttp3.*; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| /** | ||||
|  * Created by ecoolper on 2017/5/4. | ||||
|  */ | ||||
| public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<ConnectionPool, OkHttpProxyInfo> { | ||||
| public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||||
|   private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
|  | ||||
|   public OkHttpSimpleGetRequestExecutor(RequestHttp requestHttp) { | ||||
|     super(requestHttp); | ||||
| @ -19,6 +22,7 @@ public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Con | ||||
|  | ||||
|   @Override | ||||
|   public String execute(String uri, String queryParam) throws WxErrorException, IOException { | ||||
|     logger.debug("OkHttpSimpleGetRequestExecutor is running"); | ||||
|     if (queryParam != null) { | ||||
|       if (uri.indexOf('?') == -1) { | ||||
|         uri += '?'; | ||||
| @ -26,26 +30,9 @@ public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Con | ||||
|       uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | ||||
|     } | ||||
|  | ||||
|     OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient()); | ||||
|     //设置代理 | ||||
|     if (requestHttp.getRequestHttpProxy() != null) { | ||||
|       clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy()); | ||||
|     } | ||||
|     //设置授权 | ||||
|     clientBuilder.authenticator(new Authenticator() { | ||||
|       @Override | ||||
|       public Request authenticate(Route route, Response response) throws IOException { | ||||
|         String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword()); | ||||
|         return response.request().newBuilder() | ||||
|           .header("Authorization", credential) | ||||
|           .build(); | ||||
|       } | ||||
|     }); | ||||
|     //得到httpClient | ||||
|     OkHttpClient client = clientBuilder.build(); | ||||
|  | ||||
|     OkHttpClient client = requestHttp.getRequestHttpClient(); | ||||
|     Request request = new Request.Builder().url(uri).build(); | ||||
|  | ||||
|     Response response = client.newCall(request).execute(); | ||||
|     String responseContent = response.body().string(); | ||||
|     WxError error = WxError.fromJson(responseContent); | ||||
|  | ||||
| @ -5,13 +5,16 @@ import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | ||||
| import okhttp3.*; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| /** | ||||
|  * Created by ecoolper on 2017/5/4. | ||||
|  */ | ||||
| public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool, OkHttpProxyInfo> { | ||||
| public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||||
|   private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
|  | ||||
|   public OkHttpSimplePostRequestExecutor(RequestHttp requestHttp) { | ||||
|     super(requestHttp); | ||||
| @ -19,27 +22,9 @@ public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<C | ||||
|  | ||||
|   @Override | ||||
|   public String execute(String uri, String postEntity) throws WxErrorException, IOException { | ||||
|     ConnectionPool pool = requestHttp.getRequestHttpClient(); | ||||
|     final OkHttpProxyInfo proxyInfo = requestHttp.getRequestHttpProxy(); | ||||
|  | ||||
|     OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool); | ||||
|     //设置代理 | ||||
|     if (proxyInfo != null) { | ||||
|       clientBuilder.proxy(proxyInfo.getProxy()); | ||||
|     } | ||||
|     //设置授权 | ||||
|     clientBuilder.authenticator(new Authenticator() { | ||||
|       @Override | ||||
|       public Request authenticate(Route route, Response response) throws IOException { | ||||
|         String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword()); | ||||
|         return response.request().newBuilder() | ||||
|           .header("Authorization", credential) | ||||
|           .build(); | ||||
|       } | ||||
|     }); | ||||
|     logger.debug("OkHttpSimplePostRequestExecutor running"); | ||||
|     //得到httpClient | ||||
|     OkHttpClient client = clientBuilder.build(); | ||||
|  | ||||
|     OkHttpClient client = requestHttp.getRequestHttpClient(); | ||||
|  | ||||
|     MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); | ||||
|     RequestBody body = RequestBody.create(mediaType, postEntity); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 dylanleung
					dylanleung