Skip to content

Commit 037c326

Browse files
committed
issue #81 增加获取微信服务器的ip段接口
1 parent b43a62c commit 037c326

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputS
327327
*/
328328
public int invite(String userId, String inviteTips) throws WxErrorException;
329329

330+
/**
331+
* <pre>
332+
* 获取微信服务器的ip段
333+
* http://qydev.weixin.qq.com/wiki/index.php?title=回调模式#.E8.8E.B7.E5.8F.96.E5.BE.AE.E4.BF.A1.E6.9C.8D.E5.8A.A1.E5.99.A8.E7.9A.84ip.E6.AE.B5
334+
* </pre>
335+
* @return { "ip_list": ["101.226.103.*", "101.226.62.*"] }
336+
* @throws WxErrorException
337+
*/
338+
String[] getCallbackIp() throws WxErrorException;
339+
330340
/**
331341
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
332342
* @param url

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,19 @@ public int invite(String userId, String inviteTips) throws WxErrorException {
370370
return tmpJsonElement.getAsJsonObject().get("type").getAsInt();
371371
}
372372

373+
@Override
374+
public String[] getCallbackIp() throws WxErrorException {
375+
String url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip";
376+
String responseContent = get(url, null);
377+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
378+
JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
379+
String[] ips = new String[jsonArray.size()];
380+
for(int i = 0; i < jsonArray.size(); i++) {
381+
ips[i] = jsonArray.get(i).getAsString();
382+
}
383+
return ips;
384+
}
385+
373386
public String get(String url, String queryParam) throws WxErrorException {
374387
return execute(new SimpleGetRequestExecutor(), url, queryParam);
375388
}

0 commit comments

Comments
 (0)