🎨 #3834 【基础架构】小程序和开放平台两个模块补充了 Apache HttpClient 5.x 实现,同时并将多个模块的默认服务实现迁移至 Apache HttpClient 5.x 客户端

This commit is contained in:
buaazyl
2026-01-08 15:08:58 +08:00
committed by GitHub
parent e572ddfba6
commit 2c5a33917e
21 changed files with 233 additions and 22 deletions

View File

@@ -0,0 +1,74 @@
package me.chanjar.weixin.open.api.impl;
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpClientType;
import me.chanjar.weixin.common.util.http.MinishopUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.hc.DefaultHttpComponentsClientBuilder;
import me.chanjar.weixin.common.util.http.hc.HttpComponentsClientBuilder;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpHost;
import java.io.File;
/**
* httpclient 5 实现
*
* @author <a href="https://github.com/buaazyl">zhangyl</a>
*/
public class WxOpenServiceHttpComponentsImpl extends WxOpenServiceAbstractImpl<CloseableHttpClient, HttpHost> {
private CloseableHttpClient httpClient;
private HttpHost httpProxy;
@Override
public void initHttp() {
WxOpenConfigStorage configStorage = this.getWxOpenConfigStorage();
HttpComponentsClientBuilder apacheHttpClientBuilder = DefaultHttpComponentsClientBuilder.get();
apacheHttpClientBuilder.httpProxyHost(configStorage.getHttpProxyHost())
.httpProxyPort(configStorage.getHttpProxyPort())
.httpProxyUsername(configStorage.getHttpProxyUsername())
.httpProxyPassword(configStorage.getHttpProxyPassword() == null ? null :
configStorage.getHttpProxyPassword().toCharArray());
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
this.httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
}
this.httpClient = apacheHttpClientBuilder.build();
}
@Override
public CloseableHttpClient getRequestHttpClient() {
return httpClient;
}
@Override
public HttpHost getRequestHttpProxy() {
return httpProxy;
}
@Override
public HttpClientType getRequestType() {
return HttpClientType.HTTP_COMPONENTS;
}
@Override
public String get(String url, String queryParam) throws WxErrorException {
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
}
@Override
public String post(String url, String postData) throws WxErrorException {
return execute(SimplePostRequestExecutor.create(this), url, postData);
}
@Override
public WxMinishopImageUploadResult uploadMinishopMediaFile(String url, File file) throws WxErrorException {
return execute(MinishopUploadRequestExecutor.create(this), url, file);
}
}

View File

@@ -3,6 +3,6 @@ package me.chanjar.weixin.open.api.impl;
/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public class WxOpenServiceImpl extends WxOpenServiceApacheHttpClientImpl {
public class WxOpenServiceImpl extends WxOpenServiceHttpComponentsImpl {
}