判断ip是不是大陆的
1个回答
展开全部
package com.example.countrytest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/*
* 通过ip判断是否是中国内地
*/
public class IpChina {
private static final String ipUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
private static final String China = "\u4e2d\u56fd";
private static final String Taiwan = "\u53f0\u6e7e";
private static final String HongKong = "\u9999\u6e2f";
private static final String Macao = "\u6fb3\u95e8";
public static boolean ipIsChinaInland() {
try {
String json = getIpData();
System.out.println("json="+json);
JSONObject jsonObject = new JSONObject(json);
String country = jsonObject.getString("country");
String city = jsonObject.getString("province");
if(country!=null && city!=null) {
if(country.equals(China)) {
if(city.equals(Taiwan) || city.equals(HongKong) || city.equals(Macao)) {
// deal with non-china inland
} else {
// deal with china inland
return true;
}
} else {
// deal with non-china inland
}
} else {
// deal with non-china inland
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private static String getIpData() throws Exception {
HttpGet httpRequest = new HttpGet(ipUrl);// 建立http get联机
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求
if (httpResponse.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串
return null;
}
}
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/*
* 通过ip判断是否是中国内地
*/
public class IpChina {
private static final String ipUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
private static final String China = "\u4e2d\u56fd";
private static final String Taiwan = "\u53f0\u6e7e";
private static final String HongKong = "\u9999\u6e2f";
private static final String Macao = "\u6fb3\u95e8";
public static boolean ipIsChinaInland() {
try {
String json = getIpData();
System.out.println("json="+json);
JSONObject jsonObject = new JSONObject(json);
String country = jsonObject.getString("country");
String city = jsonObject.getString("province");
if(country!=null && city!=null) {
if(country.equals(China)) {
if(city.equals(Taiwan) || city.equals(HongKong) || city.equals(Macao)) {
// deal with non-china inland
} else {
// deal with china inland
return true;
}
} else {
// deal with non-china inland
}
} else {
// deal with non-china inland
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private static String getIpData() throws Exception {
HttpGet httpRequest = new HttpGet(ipUrl);// 建立http get联机
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求
if (httpResponse.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串
return null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Jtti
2024-02-02 广告
2024-02-02 广告
美国服务器大带宽可以带来以下好处:1. 传输速度快:大带宽服务器可以有效地实现双向数据同步传输,提高了数据的传输速度,同时保证了稳定性,降低了丢包率。2. 稳定性强:为了保证网站打开速度并且避免受到一定的网络攻击,选择大带宽服务器可以有效避...
点击进入详情页
本回答由Jtti提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询