java獲取客戶端登陸地址信息(國家、省份、城市等)

原文:java獲取客戶端登陸地址信息(國家、省份、城市等)

源代碼下載地址:http://www.zuidaima.com/share/1550463687658496.htm

java獲取客戶端登陸地址信息(國家、省份、城市等)

  1. /** 
  2.  * @param urlStr 
  3.  *            請求的地址 
  4.  * @param content 
  5.  * @author www.zuidaima.com 
  6.  *            請求的參數 格式爲:name=xxx&pwd=xxx 
  7.  * @param encoding 
  8.  *            服務器端請求編碼。如GBK,UTF-8等 
  9.  * @return 
  10.  */  
  11. private static String getResult(String urlStr, String content, String encoding) {  
  12.  URL url = null;  
  13.  HttpURLConnection connection = null;  
  14.  try {  
  15.      url = new URL(urlStr);  
  16.      connection = (HttpURLConnection) url.openConnection();// 新建連接實例  
  17.      connection.setConnectTimeout(2000);// 設置連接超時時間,單位毫秒  
  18.      connection.setReadTimeout(2000);// 設置讀取數據超時時間,單位毫秒  
  19.      connection.setDoOutput(true);// 是否打開輸出流 true|false  
  20.      connection.setDoInput(true);// 是否打開輸入流true|false  
  21.      connection.setRequestMethod(”POST”);// 提交方法POST|GET  
  22.      connection.setUseCaches(false);// 是否緩存true|false  
  23.      connection.connect();// 打開連接端口  
  24.      DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打開輸出流往對端服務器寫數據  
  25.      out.writeBytes(content);// 寫數據,也就是提交你的表單 name=xxx&pwd=xxx  
  26.      out.flush();// 刷新  
  27.      out.close();// 關閉輸出流  
  28.      BufferedReader reader = new BufferedReader(  
  29.            new InputStreamReader(connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據,以BufferedReader流來讀取  
  30.      StringBuffer buffer = new StringBuffer();  
  31.      String line = ”“;  
  32.      while ((line = reader.readLine()) != null) {  
  33.          buffer.append(line);  
  34.      }  
  35.      reader.close();  
  36.      return buffer.toString();  
  37.   } catch (IOException e) {  
  38.       e.printStackTrace();  
  39.   } finally {  
  40.       if (connection != null) {  
  41.           connection.disconnect();// 關閉連接  
  42.       }  
  43.   }  
  44.   return null;  
  45. }  
  46.   
  47. /** 
  48.  * @Title: decodeUnicode  
  49.  * @author kaka   
  50.  * @Description: unicode轉換成中文    
  51.  * @param @param theString 
  52.  * @param @return     
  53.  * @return String    
  54.  * @throws 
  55.  */  
  56. private static String decodeUnicode(String theString) {  
  57.  char aChar;  
  58.  int len = theString.length();  
  59.  StringBuffer outBuffer = new StringBuffer(len);  
  60.  for (int x = 0; x < len;) {  
  61.      aChar = theString.charAt(x++);  
  62.      if (aChar == ‘\\’) {  
  63.          aChar = theString.charAt(x++);  
  64.          if (aChar == ‘u’) {  
  65.              int value = 0;  
  66.              for (int i = 0; i < 4; i++) {  
  67.                   aChar = theString.charAt(x++);  
  68.                   switch (aChar) {  
  69.                   case ‘0’:  
  70.                   case ‘1’:  
  71.                   case ‘2’:  
  72.                   case ‘3’:  
  73.                   case ‘4’:  
  74.                   case ‘5’:  
  75.                   case ‘6’:  
  76.                   case ‘7’:  
  77.                   case ‘8’:  
  78.                   case ‘9’:  
  79.                       value = (value << 4) + aChar - ‘0’;  
  80.                       break;  
  81.                   case ‘a’:  
  82.                   case ‘b’:  
  83.                   case ‘c’:  
  84.                   case ‘d’:  
  85.                   case ‘e’:  
  86.                   case ‘f’:  
  87.                       value = (value << 4) + 10 + aChar - ‘a’;  
  88.                       break;  
  89.                   case ‘A’:  
  90.                   case ‘B’:  
  91.                   case ‘C’:  
  92.                   case ‘D’:  
  93.                   case ‘E’:  
  94.                   case ‘F’:  
  95.                       value = (value << 4) + 10 + aChar - ‘A’;  
  96.                       break;  
  97.                   default:  
  98.                       throw new IllegalArgumentException(“Malformed encoding.”);  
  99.                   }  
  100.              }  
  101.              outBuffer.append((char) value);  
  102.         } else {  
  103.             if (aChar == ‘t’) {  
  104.                 aChar = ’\t’;  
  105.             } else if (aChar == ‘r’) {  
  106.                 aChar = ’\r’;  
  107.             } else if (aChar == ‘n’) {  
  108.                 aChar = ’\n’;  
  109.             } else if (aChar == ‘f’) {  
  110.                 aChar = ’\f’;  
  111.             }  
  112.             outBuffer.append(aChar);  
  113.         }  
  114.     } else {  
  115.         outBuffer.append(aChar);  
  116.     }  
  117.  }  
  118.  return outBuffer.toString();  
  119.   
  120. /** 
  121.  *  
  122.  * @param content 
  123.  *            請求的參數 格式爲:name=xxx&pwd=xxx 
  124.  * @param encoding 
  125.  *            服務器端請求編碼。如GBK,UTF-8等 
  126.  * @return 
  127.  * @throws UnsupportedEncodingException 
  128.  */  
  129. public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException {  
  130.  String result = null;  
  131.   StringBuffer sb = new StringBuffer();  
  132.  // 這裏調用pconline的接口  
  133.  String urlStr = ”http://ip.taobao.com/service/getIpInfo.php”;  
  134.  // 從http://whois.pconline.com.cn取得IP所在的省市區信息  
  135.  String returnStr = getResult(urlStr, content, encodingString);  
  136.      if (returnStr != null) {  
  137.          // 處理返回的省市區信息  
  138.      String[] temp = returnStr.split(”,”);  
  139.      if(temp.length<3){  
  140.          return “0”;//無效IP,局域網測試  
  141.      }  
  142.       
  143.      String country = ”“;   
  144.      String area = ”“;   
  145.      String region = ”“;   
  146.      String city = ”“;   
  147.      String county = ”“;   
  148.      String isp = ”“;   
  149.      Boolean flag = false;  
  150.      for(int i=0;i<temp.length;i++){  
  151.          if(flag){  
  152.              return result;  
  153.          }  
  154.          switch(i){   
  155.          case 1:  
  156.              country = (temp[i].split(”:”))[2].replaceAll(“\”““”);   
  157.              country = decodeUnicode(country);//國家   
  158.              if(country.equals(“未分配或者內網IP”)){  
  159.                  flag = true;  
  160.                  sb.append(country);  
  161.                  result = sb.toString();  
  162.                  break;  
  163.              }  
  164.              sb.append(”國家:”);  
  165.              sb.append(country);  
  166.              sb.append(”,”);  
  167.              break;   
  168.          case 3:  
  169.              area =(temp[i].split(”:”))[1].replaceAll(“\”““”);   
  170.              area = decodeUnicode(area);//地區   
  171.              break;   
  172.          case 5:  
  173.              region = (temp[i].split(”:”))[1].replaceAll(“\”““”);   
  174.              region = decodeUnicode(region);//省份   
  175.              sb.append(”省份:”);  
  176.              sb.append(region);  
  177.              sb.append(”,”);  
  178.              break;   
  179.          case 7:  
  180.              city = (temp[i].split(”:”))[1].replaceAll(“\”““”);   
  181.              city = decodeUnicode(city);//市區   
  182.              sb.append(”城市:”);  
  183.              sb.append(city);  
  184.              sb.append(”,”);  
  185.              break;   
  186.          case 9:  
  187.              county = (temp[i].split(”:”))[1].replaceAll(“\”““”);   
  188.              county = decodeUnicode(county);//地區  
  189.              sb.append(”地區:”);  
  190.              sb.append(county);  
  191.              break;   
  192.          case 11:  
  193.              isp = (temp[i].split(”:”))[1].replaceAll(“\”““”);   
  194.              isp = decodeUnicode(isp);//ISP公司   
  195.              break;   
  196.          }   
  197.     }  
  198.     result = sb.toString();  
  199.      }  
  200.      return result;  
  201. }  
 /**
  * @param urlStr
  *            請求的地址
  * @param content
  * @author www.zuidaima.com
  *            請求的參數 格式爲:name=xxx&pwd=xxx
  * @param encoding
  *            服務器端請求編碼。如GBK,UTF-8等
  * @return
  */
 private static String getResult(String urlStr, String content, String encoding) {
     URL url = null;
     HttpURLConnection connection = null;
     try {
         url = new URL(urlStr);
         connection = (HttpURLConnection) url.openConnection();// 新建連接實例
         connection.setConnectTimeout(2000);// 設置連接超時時間,單位毫秒
         connection.setReadTimeout(2000);// 設置讀取數據超時時間,單位毫秒
         connection.setDoOutput(true);// 是否打開輸出流 true|false
         connection.setDoInput(true);// 是否打開輸入流true|false
         connection.setRequestMethod("POST");// 提交方法POST|GET
         connection.setUseCaches(false);// 是否緩存true|false
         connection.connect();// 打開連接端口
         DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打開輸出流往對端服務器寫數據
         out.writeBytes(content);// 寫數據,也就是提交你的表單 name=xxx&pwd=xxx
         out.flush();// 刷新
         out.close();// 關閉輸出流
         BufferedReader reader = new BufferedReader(
               new InputStreamReader(connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據,以BufferedReader流來讀取
         StringBuffer buffer = new StringBuffer();
         String line = "";
         while ((line = reader.readLine()) != null) {
             buffer.append(line);
         }
         reader.close();
         return buffer.toString();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          if (connection != null) {
              connection.disconnect();// 關閉連接
          }
      }
      return null;
 }

 /**
  * @Title: decodeUnicode 
  * @author kaka  
  * @Description: unicode轉換成中文   
  * @param @param theString
  * @param @return    
  * @return String   
  * @throws
  */
 private static String decodeUnicode(String theString) {
     char aChar;
     int len = theString.length();
     StringBuffer outBuffer = new StringBuffer(len);
     for (int x = 0; x < len;) {
         aChar = theString.charAt(x++);
         if (aChar == '\\') {
             aChar = theString.charAt(x++);
             if (aChar == 'u') {
                 int value = 0;
                 for (int i = 0; i < 4; i++) {
                      aChar = theString.charAt(x++);
                      switch (aChar) {
                      case '0':
                      case '1':
                      case '2':
                      case '3':
                      case '4':
                      case '5':
                      case '6':
                      case '7':
                      case '8':
                      case '9':
                          value = (value << 4) + aChar - '0';
                          break;
                      case 'a':
                      case 'b':
                      case 'c':
                      case 'd':
                      case 'e':
                      case 'f':
                          value = (value << 4) + 10 + aChar - 'a';
                          break;
                      case 'A':
                      case 'B':
                      case 'C':
                      case 'D':
                      case 'E':
                      case 'F':
                          value = (value << 4) + 10 + aChar - 'A';
                          break;
                      default:
                          throw new IllegalArgumentException("Malformed encoding.");
                      }
                 }
                 outBuffer.append((char) value);
            } else {
                if (aChar == 't') {
                    aChar = '\t';
                } else if (aChar == 'r') {
                    aChar = '\r';
                } else if (aChar == 'n') {
                    aChar = '\n';
                } else if (aChar == 'f') {
                    aChar = '\f';
                }
                outBuffer.append(aChar);
            }
        } else {
            outBuffer.append(aChar);
        }
     }
     return outBuffer.toString();
}
 /**
  * 
  * @param content
  *            請求的參數 格式爲:name=xxx&pwd=xxx
  * @param encoding
  *            服務器端請求編碼。如GBK,UTF-8等
  * @return
  * @throws UnsupportedEncodingException
  */
 public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException {
     String result = null;
      StringBuffer sb = new StringBuffer();
     // 這裏調用pconline的接口
     String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
     // 從http://whois.pconline.com.cn取得IP所在的省市區信息
     String returnStr = getResult(urlStr, content, encodingString);
     if (returnStr != null) {
         // 處理返回的省市區信息
         String[] temp = returnStr.split(",");
         if(temp.length<3){
             return "0";//無效IP,局域網測試
         }

         String country = ""; 
         String area = ""; 
         String region = ""; 
         String city = ""; 
         String county = ""; 
         String isp = ""; 
         Boolean flag = false;
         for(int i=0;i<temp.length;i++){
             if(flag){
                 return result;
             }
             switch(i){ 
             case 1:
                 country = (temp[i].split(":"))[2].replaceAll("\"", ""); 
                 country = decodeUnicode(country);//國家 
                 if(country.equals("未分配或者內網IP")){
                     flag = true;
                     sb.append(country);
                     result = sb.toString();
                     break;
                 }
                 sb.append("國家:");
                 sb.append(country);
                 sb.append(",");
                 break; 
             case 3:
                 area =(temp[i].split(":"))[1].replaceAll("\"", ""); 
                 area = decodeUnicode(area);//地區 
                 break; 
             case 5:
                 region = (temp[i].split(":"))[1].replaceAll("\"", ""); 
                 region = decodeUnicode(region);//省份 
                 sb.append("省份:");
                 sb.append(region);
                 sb.append(",");
                 break; 
             case 7:
                 city = (temp[i].split(":"))[1].replaceAll("\"", ""); 
                 city = decodeUnicode(city);//市區 
                 sb.append("城市:");
                 sb.append(city);
                 sb.append(",");
                 break; 
             case 9:
                 county = (temp[i].split(":"))[1].replaceAll("\"", ""); 
                 county = decodeUnicode(county);//地區
                 sb.append("地區:");
                 sb.append(county);
                 break; 
             case 11:
                 isp = (temp[i].split(":"))[1].replaceAll("\"", ""); 
                 isp = decodeUnicode(isp);//ISP公司 
                 break; 
             } 
        }
        result = sb.toString();
     }
     return result;
 }

發佈了60 篇原創文章 · 獲贊 14 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章