当前位置:网站首页>腾讯根据ip解析地址

腾讯根据ip解析地址

2022-04-23 14:00:00 白云碎里一蓑舟

//需向腾讯申请key
private static String TXURL = "https://apis.map.qq.com/ws/location/v1/ip?key=&ip=";
public static String getTXCityCodeByIp(String ip) {
    
                CloseableHttpClient httpClient = HttpClients.createDefault();
                try {
    
                        String requestURL = String.format("%s%s", TXURL, ip);
                        HttpGet getMethod = new HttpGet(requestURL);
                        HttpResponse response = httpClient.execute(getMethod);
                        if (null != response && response.getStatusLine().getStatusCode() == 200) {
    
                                return EntityUtils.toString(response.getEntity(), "UTF-8");
                        }
                } catch (Exception e) {
    
                        log.error("getTXCityCodeByIp ip:[{}] is Exception:{}", ip, e.toString());
                } finally {
    
                        try {
    
                                httpClient.close();
                        } catch (IOException e) {
    
                                log.error(e.toString());
                        }
                }
                return null;
        }
        
        public static void main(String[] args) {
    
                String ip = "";
                System.out.println(getTXCityCodeByIp(ip));
        }

版权声明
本文为[白云碎里一蓑舟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_49513507/article/details/124324558