From 2597049f0525d57e452e63e165f4f4a797ade888 Mon Sep 17 00:00:00 2001 From: tangliu Date: Mon, 12 Jul 2021 19:19:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96AccessToken?= =?UTF-8?q?=E6=97=B6ApiHostUrl=E6=9C=AA=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java | 8 +++++++- .../wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java | 7 ++++++- .../wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java index c69772a5d8..6914977861 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java @@ -6,6 +6,7 @@ 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 org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; @@ -60,7 +61,12 @@ public HttpType getRequestType() { @Override protected String doGetAccessTokenRequest() throws IOException { - String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); + + String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ? + WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) : + WxMaService.GET_ACCESS_TOKEN_URL; + + url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); HttpGet httpGet = null; CloseableHttpResponse response = null; diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java index 984d0d3fe6..90ee6516ae 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java @@ -7,6 +7,7 @@ import jodd.http.ProxyInfo; import jodd.http.net.SocketHttpConnectionProvider; import me.chanjar.weixin.common.util.http.HttpType; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; @@ -45,7 +46,11 @@ public HttpType getRequestType() { @Override protected String doGetAccessTokenRequest() throws IOException { - String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); + String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ? + WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) : + WxMaService.GET_ACCESS_TOKEN_URL; + + url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); HttpRequest request = HttpRequest.get(url); if (this.getRequestHttpProxy() != null) { SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java index a36444d97a..0fa49d9d07 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java @@ -5,6 +5,7 @@ import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import okhttp3.*; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.util.Objects; @@ -63,7 +64,11 @@ public HttpType getRequestType() { @Override protected String doGetAccessTokenRequest() throws IOException { - String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); + String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ? + WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) : + WxMaService.GET_ACCESS_TOKEN_URL; + + url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); Request request = new Request.Builder().url(url).get().build(); try (Response response = getRequestHttpClient().newCall(request).execute()) { return Objects.requireNonNull(response.body()).string();