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));
}
|