import com.cdeledu.plat.fin.common.util.URLUtil;
import com.cdeledu.plat.fin.common.util.WebPage;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class IpLookUp
{
private static String sinaURL = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=";
public static String sinaIpLookup(String ip)
{
String pageURL = sinaURL;
WebPage webPage = null;
String json = "";
webPage = URLUtil.getPageContent(pageURL + ip);
if(webPage != null && webPage.getHtml() != null)
{
json = webPage.getHtml();
String begin = "{";
String end = "}";
json = json.substring(json.indexOf(begin), json.indexOf(end) + 1);
}
return json;
}
public static IpInfo parserSinaLoopkUpIpInfo(String json)
{
Gson gson = new Gson();
IpInfo ipInfo = null;
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(json);
if(jsonElement.isJsonObject())
{
JsonObject JsonObject = (JsonObject)jsonElement;
ipInfo = gson.fromJson(JsonObject, IpInfo.class);
if(ipInfo != null)
{
System.out.println(ipInfo.getStart() + "\t" + ipInfo.getEnd() + "\t" + ipInfo.getProvince());
}
}
return ipInfo;
}
/**
* @param args
*/
public static void main(String[] args)
{
String ip = "219.232.43.228";
ip = "218.94.132.146";
long start = System.currentTimeMillis();
String json = sinaIpLookup(ip);
IpInfo ipInfo = parserSinaLoopkUpIpInfo(json);
System.out.println(ipInfo);
System.out.println("===times===" + (System.currentTimeMillis() - start));
}