码迷,mamicode.com
首页 > 编程语言 > 详细

java查看IP所属地区及哪个运营商

时间:2015-06-29 14:54:20      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;

public class TestIp {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://apistore.baidu.com/microservice/iplookup?ip=106.6.104.72");
		URLConnection con = url.openConnection();
		InputStream is = con.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		StringBuffer buffer = new StringBuffer();
		String line =null;
		while(null != (line = br.readLine()))
		{
			buffer.append(line);
		}
		br.close();
		isr.close();
		is.close();
		System.out.println(buffer.toString());
		
		JSONObject jsonObject = JSONObject.fromObject(buffer.toString());
		System.out.println(jsonObject);
		JSONObject jsonObject1 = JSONObject.fromObject(jsonObject.get("retData"));
		System.out.println(jsonObject1.get("province").toString()+jsonObject1.get("city").toString()+ "(" +jsonObject1.get("carrier").toString() + ")");
	}
}

java查看IP所属地区及哪个运营商

标签:

原文地址:http://blog.csdn.net/u013498057/article/details/46681729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!