Skip to content

Commit fb10acf

Browse files
committed
issue #101 获取微信服务器IP地址
1 parent fd24b53 commit fb10acf

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ public interface WxMpService {
421421
*/
422422
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);
423423

424+
/**
425+
* <pre>
426+
* 获取微信服务器IP地址
427+
* http://mp.weixin.qq.com/wiki/0/2ad4b6bfd29f30f71d39616c2a0fcedc.html
428+
* </pre>
429+
* @return
430+
* @throws WxErrorException
431+
*/
432+
String[] getCallbackIP() throws WxErrorException;
433+
424434
/**
425435
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
426436
* @param url

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.gson.internal.Streams;
@@ -451,6 +452,19 @@ public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken
451452
return true;
452453
}
453454

455+
@Override
456+
public String[] getCallbackIP() throws WxErrorException {
457+
String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
458+
String responseContent = get(url, null);
459+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
460+
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
461+
String[] ipArray = new String[ipList.size()];
462+
for (int i = 0; i < ipList.size(); i++) {
463+
ipArray[i] = ipList.get(i).getAsString();
464+
}
465+
return ipArray;
466+
}
467+
454468
public String get(String url, String queryParam) throws WxErrorException {
455469
return execute(new SimpleGetRequestExecutor(), url, queryParam);
456470
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.common.api.WxConsts;
5+
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
6+
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
8+
import me.chanjar.weixin.mp.bean.WxMpMassNews;
9+
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
10+
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
11+
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
12+
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
13+
import org.testng.Assert;
14+
import org.testng.annotations.DataProvider;
15+
import org.testng.annotations.Guice;
16+
import org.testng.annotations.Test;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.Arrays;
21+
22+
/**
23+
*
24+
* @author chanjarster
25+
*
26+
*/
27+
@Test(groups = "miscAPI", dependsOnGroups = { "baseAPI"})
28+
@Guice(modules = ApiTestModule.class)
29+
public class WxMpMiscAPITest {
30+
31+
@Inject
32+
protected WxMpServiceImpl wxService;
33+
34+
@Test
35+
public void getCallbackIP() throws WxErrorException {
36+
String[] ipArray = wxService.getCallbackIP();
37+
System.out.println(Arrays.toString(ipArray));
38+
Assert.assertNotNull(ipArray);
39+
Assert.assertNotEquals(ipArray.length, 0);
40+
}
41+
42+
}

weixin-java-mp/src/test/resources/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<class name="me.chanjar.weixin.mp.api.WxMpShortUrlAPITest" />
1616
<class name="me.chanjar.weixin.mp.api.WxMpMessageRouterTest" />
1717
<class name="me.chanjar.weixin.mp.api.WxMpJsAPITest" />
18+
<class name="me.chanjar.weixin.mp.api.WxMpMiscAPITest" />
1819
</classes>
1920
</test>
2021

0 commit comments

Comments
 (0)