mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	issue #101 获取微信服务器IP地址
This commit is contained in:
		| @ -421,6 +421,16 @@ public interface WxMpService { | ||||
|    */ | ||||
|   public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|    * 获取微信服务器IP地址 | ||||
|    * http://mp.weixin.qq.com/wiki/0/2ad4b6bfd29f30f71d39616c2a0fcedc.html | ||||
|    * </pre> | ||||
|    * @return | ||||
|    * @throws WxErrorException | ||||
|    */ | ||||
|   String[] getCallbackIP() throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求 | ||||
|    * @param url | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| package me.chanjar.weixin.mp.api; | ||||
|  | ||||
| import com.google.gson.JsonArray; | ||||
| import com.google.gson.JsonElement; | ||||
| import com.google.gson.JsonObject; | ||||
| import com.google.gson.internal.Streams; | ||||
| @ -451,6 +452,19 @@ public class WxMpServiceImpl implements WxMpService { | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String[] getCallbackIP() throws WxErrorException { | ||||
|     String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip"; | ||||
|     String responseContent = get(url, null); | ||||
|     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||
|     JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray(); | ||||
|     String[] ipArray = new String[ipList.size()]; | ||||
|     for (int i = 0; i < ipList.size(); i++) { | ||||
|       ipArray[i] = ipList.get(i).getAsString(); | ||||
|     } | ||||
|     return ipArray; | ||||
|   } | ||||
|  | ||||
|   public String get(String url, String queryParam) throws WxErrorException { | ||||
|     return execute(new SimpleGetRequestExecutor(), url, queryParam); | ||||
|   } | ||||
|  | ||||
| @ -0,0 +1,42 @@ | ||||
| package me.chanjar.weixin.mp.api; | ||||
|  | ||||
| import com.google.inject.Inject; | ||||
| import me.chanjar.weixin.common.api.WxConsts; | ||||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpMassNews; | ||||
| import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpMassVideo; | ||||
| import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | ||||
| import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | ||||
| import org.testng.Assert; | ||||
| import org.testng.annotations.DataProvider; | ||||
| import org.testng.annotations.Guice; | ||||
| import org.testng.annotations.Test; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.util.Arrays; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author chanjarster | ||||
|  * | ||||
|  */ | ||||
| @Test(groups = "miscAPI", dependsOnGroups = { "baseAPI"}) | ||||
| @Guice(modules = ApiTestModule.class) | ||||
| public class WxMpMiscAPITest { | ||||
|  | ||||
|   @Inject | ||||
|   protected WxMpServiceImpl wxService; | ||||
|  | ||||
|   @Test | ||||
|   public void getCallbackIP() throws WxErrorException { | ||||
|     String[] ipArray = wxService.getCallbackIP(); | ||||
|     System.out.println(Arrays.toString(ipArray)); | ||||
|     Assert.assertNotNull(ipArray); | ||||
|     Assert.assertNotEquals(ipArray.length, 0); | ||||
|   } | ||||
|  | ||||
| } | ||||
| @ -15,6 +15,7 @@ | ||||
| 			<class name="me.chanjar.weixin.mp.api.WxMpShortUrlAPITest" /> | ||||
| 			<class name="me.chanjar.weixin.mp.api.WxMpMessageRouterTest" /> | ||||
|       <class name="me.chanjar.weixin.mp.api.WxMpJsAPITest" /> | ||||
|       <class name="me.chanjar.weixin.mp.api.WxMpMiscAPITest" /> | ||||
| 		</classes> | ||||
| 	</test> | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian