代码
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* 根据IP地址获取详细的地域信息
*
* @author Yawei Xi
* @date 2018-10-13
*/
public class IPUtil {
private static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
public static Ip getIpDataByIp(String ip) {
String data = sendGet(IP_URL + "?ip=" + ip);
if (null == data) {
return null;
}
Result result = JSON.parseObject(data, Result.class);
if (null == result || result.getCode() != 0) {
return null;
}
return result.getData();
}
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
// Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
/*for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
} */
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream())); //这里如果出现乱码,请使用带编码的InputStreamReader构造方法,将需要的编码设置进去</span>
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static class Result {
private Integer code;
private Ip data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Ip getData() {
return data;
}
public void setData(Ip data) {
this.data = data;
}
}
public static class Ip {
private String ip;
private String country;
private String area;
private String region;
private String city;
private String county;
private String isp;
private String countryId;
private String areaId;
private String regionId;
private String cityId;
private String countyId;
private String ispId;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getIsp() {
return isp;
}
public void setIsp(String isp) {
this.isp = isp;
}
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
}
public String getAreaId() {
return areaId;
}
public void setAreaId(String areaId) {
this.areaId = areaId;
}
public String getRegionId() {
return regionId;
}
public void setRegionId(String regionId) {
this.regionId = regionId;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCountyId() {
return countyId;
}
public void setCountyId(String countyId) {
this.countyId = countyId;
}
public String getIspId() {
return ispId;
}
public void setIspId(String ispId) {
this.ispId = ispId;
}
}
}
测试用例
/**
* @author Yawei Xi
* @date 2018-10-13
*/
public class Test {
public static void main(String[] args) {
String ip = "140.206.194.105";
IPUtil.Ip data = IPUtil.getIpDataByIp(ip);
System.out.println("IP:" + data.getIp());
System.out.println("城市:" + data.getCity());
System.out.println("网络服务商:" + data.getIsp());
}
}
测试结果
IP:140.206.194.105
城市:上海
网络服务商:联通