分享

通过JAVA bean查询ip归属地,身份证号码信息,手机号码归属地

 zwmnhao1980 2012-03-07
  1. package test;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8. import java.util.regex.Pattern;  
  9.   
  10. //获取Ip所在地: http://www.youdao.com/smartresult-xml/search.s?type=ip&q=58.30.32.61<!---->  
  11.   
  12. //身份证信息: http://www.youdao.com/smartresult-xml/search.s?type=id&q=232700198910206016<!---->  
  13.   
  14. //获取手机所在地: http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=13671151172<!---->  
  15.   
  16. public class Test {  
  17.     static Pattern patternLocation = Pattern  
  18.             .compile("<LOCATION>(.+{1,})</LOCATION>");  
  19.     private static final String IPURL = " http://www.youdao.com/smartresult-xml/search.s?type=ip&q=";  
  20.     private static final String IDURL = " http://www.youdao.com/smartresult-xml/search.s?type=id&q=";  
  21.     private static final String MOBILEURL = " http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=";  
  22.   
  23.     private static String getLocationByIP(String ip) {  
  24.         String address = "";  
  25.         try {  
  26.   
  27.             URL url = new URL(IPURL + ip);  
  28.             address = search(url);  
  29.   
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         address = address.substring(address.indexOf("location") + 9);  
  34.         return address.substring(0, address  
  35.                 .indexOf("</location"));  
  36.     }  
  37.       
  38.     private static String getLocationById(String id) {  
  39.         String address = "";  
  40.         try {  
  41.   
  42.             URL url = new URL(IDURL + id);  
  43.             address = search(url);  
  44.   
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.         String sex = address.indexOf("<gender>m</gender")>0?"男":"女";  
  49.           
  50.         address = address.substring(address.indexOf("location") + 9);  
  51.         String birthday = address.substring(address.indexOf("birthday>")+9,address.indexOf("</bir"));  
  52.         birthday = birthday.substring(0,4)+"年"+birthday.substring(4,6)+"月"+birthday.substring(6,8)+"日";  
  53.         return "地址:"+address.substring(0, address  
  54.                 .indexOf("</location"))+" 性别:"+sex+" 生日:"+birthday;  
  55.     }  
  56.       
  57.     private static String getLocationByMobile(String mobile) {  
  58.         String address = "";  
  59.         try {  
  60.   
  61.             URL url = new URL(MOBILEURL + mobile);  
  62.             address = search(url);  
  63.   
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.         address = address.substring(address.indexOf("location") + 9);  
  68.         return "该号码归属地为:"+address.substring(0, address  
  69.                 .indexOf("</location"));  
  70.     }  
  71.     private static String search(URL url) throws IOException {  
  72.         String address;  
  73.         HttpURLConnection connect = (HttpURLConnection) url  
  74.                 .openConnection();  
  75.         InputStream is = connect.getInputStream();  
  76.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  77.         byte[] buff = new byte[256];  
  78.         int rc = 0;  
  79.         while ((rc = is.read(buff, 0256)) > 0) {  
  80.             outStream.write(buff, 0, rc);  
  81.         }  
  82.         byte[] b = outStream.toByteArray();  
  83.         //关闭  
  84.         outStream.close();  
  85.         is.close();  
  86.         connect.disconnect();  
  87.         address = new String(b);  
  88.         return address;  
  89.     }  
  90.   
  91.     public static void main(String[] args) {  
  92.         //System.out.println(getLocationByIP("221.226.177.158"));  
  93.         System.out.println(getLocationById("321281198710093696"));  
  94.         System.out.println(getLocationByMobile("15895861841"));  
  95.     }  
  96. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多