Skip to content

Commit 25eb592

Browse files
committed
fix: #172 based on PR #259
1 parent d759e69 commit 25eb592

File tree

5 files changed

+150
-53
lines changed

5 files changed

+150
-53
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<httpclient.version>4.5</httpclient.version>
4545
<slf4j.version>1.7.10</slf4j.version>
4646
<logback.version>1.1.2</logback.version>
47+
<jodd-http.version>3.6.7</jodd-http.version>
4748
</properties>
4849

4950
<dependencies>
@@ -68,6 +69,11 @@
6869
<artifactId>httpmime</artifactId>
6970
<version>${httpclient.version}</version>
7071
</dependency>
72+
<dependency>
73+
<groupId>org.jodd</groupId>
74+
<artifactId>jodd-http</artifactId>
75+
<version>${jodd-http.version}</version>
76+
</dependency>
7177
<dependency>
7278
<groupId>com.google.code.gson</groupId>
7379
<artifactId>gson</artifactId>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
import jodd.http.HttpRequest;
4+
import jodd.http.HttpResponse;
5+
import jodd.http.ProxyInfo;
6+
import jodd.http.net.SocketHttpConnectionProvider;
7+
import me.chanjar.weixin.common.bean.result.WxError;
8+
import me.chanjar.weixin.common.exception.WxErrorException;
9+
import org.apache.http.HttpHost;
10+
import org.apache.http.impl.client.CloseableHttpClient;
11+
12+
import java.io.IOException;
13+
14+
/**
15+
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String
16+
*
17+
* @author Daniel Qian
18+
*/
19+
public class JoddGetRequestExecutor implements RequestExecutor<String, String> {
20+
21+
@Override
22+
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
23+
String queryParam) throws WxErrorException, IOException {
24+
if (queryParam != null) {
25+
if (uri.indexOf('?') == -1) {
26+
uri += '?';
27+
}
28+
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
29+
}
30+
31+
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
32+
33+
if (httpProxy != null) {
34+
ProxyInfo proxyInfoObj = new ProxyInfo(
35+
ProxyInfo.ProxyType.HTTP,
36+
httpProxy.getAddress().getHostAddress(),
37+
httpProxy.getPort(), "", "");
38+
provider.useProxy(proxyInfoObj);
39+
}
40+
41+
HttpRequest request = HttpRequest.get(uri);
42+
request.method("GET");
43+
request.charset("UTF-8");
44+
45+
HttpResponse response = request.open(provider).send();
46+
String result = response.bodyText();
47+
48+
WxError error = WxError.fromJson(result);
49+
if (error.getErrorCode() != 0) {
50+
throw new WxErrorException(error);
51+
}
52+
return result;
53+
}
54+
55+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
import jodd.http.HttpRequest;
4+
import jodd.http.HttpResponse;
5+
import jodd.http.ProxyInfo;
6+
import jodd.http.net.SocketHttpConnectionProvider;
7+
import me.chanjar.weixin.common.bean.result.WxError;
8+
import me.chanjar.weixin.common.exception.WxErrorException;
9+
import org.apache.http.HttpHost;
10+
import org.apache.http.impl.client.CloseableHttpClient;
11+
12+
import java.io.IOException;
13+
14+
/**
15+
* 简单的POST请求执行器,请求的参数是String, 返回的结果也是String
16+
*
17+
* @author Edison Guo
18+
*/
19+
public class JoddPostRequestExecutor implements RequestExecutor<String, String> {
20+
21+
@Override
22+
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
23+
String postEntity) throws WxErrorException, IOException {
24+
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
25+
26+
if (httpProxy != null) {
27+
ProxyInfo proxyInfoObj = new ProxyInfo(
28+
ProxyInfo.ProxyType.HTTP,
29+
httpProxy.getAddress().getHostAddress(),
30+
httpProxy.getPort(), "", "");
31+
provider.useProxy(proxyInfoObj);
32+
}
33+
34+
HttpRequest request = HttpRequest.get(uri);
35+
request.method("POST");
36+
request.charset("UTF-8");
37+
request.bodyText(postEntity);
38+
39+
HttpResponse response = request.open(provider).send();
40+
String result = response.bodyText();
41+
42+
WxError error = WxError.fromJson(result);
43+
if (error.getErrorCode() != 0) {
44+
throw new WxErrorException(error);
45+
}
46+
return result;
47+
}
48+
49+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
2626
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
2727
import me.chanjar.weixin.common.util.http.RequestExecutor;
28-
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
29-
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
28+
import me.chanjar.weixin.common.util.http.JoddGetRequestExecutor;
29+
import me.chanjar.weixin.common.util.http.JoddPostRequestExecutor;
3030
import me.chanjar.weixin.common.util.http.URIUtil;
3131
import me.chanjar.weixin.common.util.json.GsonHelper;
3232
import me.chanjar.weixin.cp.bean.WxCpDepart;
@@ -156,7 +156,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
156156
synchronized (globalJsapiTicketRefreshLock) {
157157
if (wxCpConfigStorage.isJsapiTicketExpired()) {
158158
String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
159-
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
159+
String responseContent = execute(new JoddGetRequestExecutor(), url, null);
160160
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
161161
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
162162
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
@@ -256,7 +256,7 @@ public File mediaDownload(String media_id) throws WxErrorException {
256256
public Integer departCreate(WxCpDepart depart) throws WxErrorException {
257257
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
258258
String responseContent = execute(
259-
new SimplePostRequestExecutor(),
259+
new JoddPostRequestExecutor(),
260260
url,
261261
depart.toJson());
262262
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
@@ -512,11 +512,11 @@ public String[] getCallbackIp() throws WxErrorException {
512512
}
513513

514514
public String get(String url, String queryParam) throws WxErrorException {
515-
return execute(new SimpleGetRequestExecutor(), url, queryParam);
515+
return execute(new JoddGetRequestExecutor(), url, queryParam);
516516
}
517517

518518
public String post(String url, String postData) throws WxErrorException {
519-
return execute(new SimplePostRequestExecutor(), url, postData);
519+
return execute(new JoddPostRequestExecutor(), url, postData);
520520
}
521521

522522
/**
@@ -685,12 +685,5 @@ public void setTmpDirFile(File tmpDirFile) {
685685
this.tmpDirFile = tmpDirFile;
686686
}
687687

688-
public static void main(String[] args) {
689-
Float a = 3.1f;
690-
System.out.println(3.1d);
691-
System.out.println(new BigDecimal(3.1d));
692-
System.out.println(new BigDecimal(a));
693-
System.out.println(a.toString());
694-
System.out.println(a.doubleValue());
695-
}
688+
696689
}

0 commit comments

Comments
 (0)