现在的位置: 首页 > 综合 > 正文

实现IP到地理位置的反查功能

2014年02月01日 ⁄ 综合 ⁄ 共 730字 ⁄ 字号 评论关闭
//得到用户注册地区
	public String getAddreById(String ip){
	       StringBuffer result = new StringBuffer();  
	        try {  
	            URL url = new URL("http://whois.pconline.com.cn/ip.jsp?ip=" + ip);  
	            HttpURLConnection connect = (HttpURLConnection)url.openConnection();  
	            InputStream is = connect.getInputStream();  
	            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
	            byte[] buff = new byte[256];  
	            int rc = 0;  
	            while ((rc = is.read(buff, 0, 256)) > 0) {  
	                outStream.write(buff, 0, rc);  
	            }  
	            byte[] b = outStream.toByteArray();  
	            // 关闭  
	            outStream.close();  
	            is.close();  
	            connect.disconnect();  
	            // 因为提供服务的网站使用GB2312编码,所以转化为字符串时也是GB2312,否则是乱码!
	            String aa = new String(b, "gb2312");  
	            result.append(aa);  
	  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	            result = null;  
	        }  
	        return result.toString(); 
	}

http://syc001.iteye.com/blog/1100828 

http://www.iteye.com/topic/696261

抱歉!评论已关闭.