asdtiang的博客 感謝blogjava提供的博客交流平臺

          java 根據IP獲取所在地

          Posted on 2010-08-12 16:21 asdtiang 閱讀(2073) 評論(1)  編輯  收藏 所屬分類: JAVA

          根據IP獲取所在地及服務商:
          直接給代碼吧:
          根據這個網站http://www.ip138.com為獲取地址。
          訪問地址為:http://www.ip138.com/ips.asp?ip=124.236.239.105&action=2
          構造connection,返回里直接字符串處理的,主要是自己的正則表達式沒學好啊
          返回中:

          <td align="center"><ul class="ul1"><li>本站主數據:河北省石家莊市 電信</li><li>參考數據一:河北省石家莊市 電信</li><li>參考數據二:河北省石家莊市 電信</li></ul></td>
          

          結果只取了主數據。

          public ? class ?GetIPArea? {
          ????
          ????
          // URL
          ???? private ? static ? final ?String?REQUEST_URL = " http://www.ip138.com/ips.asp " ;
          ????? ///////有時查詢不出來,就用這個URL??? http://www.ip138.com/ips8.asp
          ????
          //
          ???? private ? static ? final ?String?REQUEST_MOTHOD = " POST " ;
          ????
          // ????URL??HttpURLConnection????
          ???? private ? static ?HttpURLConnection?httpConn = null ;
          ????
          ????
          /**
          ?????*?IP
          ?????*?
          @return ?String
          ?????
          */

          ????
          public ? static ?String?getIPArea(String?ip)
          ????
          {
          ????????String?requestParameter
          = " ip= " + ip + " &action=2 " ;
          ????????String?IPArea
          = "" ;
          ????????BufferedReader?br
          = null ;
          ????????
          try
          ????????
          {
          ????????????httpConn
          = (HttpURLConnection) new ?URL(REQUEST_URL).openConnection();
          ????????????httpConn.setRequestMethod(REQUEST_MOTHOD);
          ????????????httpConn.setDoOutput(
          true );
          ????????????httpConn.getOutputStream().write(requestParameter.getBytes());
          ????????????httpConn.getOutputStream().flush();
          ????????????httpConn.getOutputStream().close();
          ????????????
          ????????????br
          = new ?BufferedReader( new ?InputStreamReader(httpConn.getInputStream(), " gb2312 " ));
          ????????????String?lineStr
          = null ;
          ????????????
          while ((lineStr = br.readLine()) != null )
          ????????????
          {
          ????????????????
          if (lineStr.contains( " <td?align=\ " center\ " ><ul?class=\ " ul1\ " ><li> " ))
          ????????????????
          {
          ????????????????????IPArea
          = lineStr.substring(lineStr.indexOf( " " ) + 1 ,lineStr.indexOf( " </ " ));
          ????????????????????
          break ;
          ????????????????}

          ????????????}

          ????????}

          ????????
          catch (IOException?e)? {
          ????????????e.printStackTrace();
          ????????}

          ????????
          finally
          ????????
          {
          ????????????
          if (br != null )
          ????????????????
          try ? {
          ????????????????????br.close();
          ????????????????}
          ? catch ?(IOException?e)? {
          ????????????????????e.printStackTrace();
          ????????????????}

          ????????}

          ????????
          return ?IPArea;
          ????}

          ????
          public ? static ? void ?main(String?args[]) {
          ????????System.out.println(GetIPArea.getIPArea(
          " 124.236.239.105 " ));
          ????}

          }

          http編碼問題在這里也順便記一下。
          br=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"gb2312"));//在這個地方加上編碼

          這種方法獲取IP很不穩定,容易獲取不到,我測試是這樣的,最好的方法還是有自己的IP數據庫。
          純真IP數據庫剛好可以用上。
          具體參考:http://javagp.group.javaeye.com/group/topic/11866
          在他的博客上有源碼下載,直接用就可以了





          主要參考?? http://gzhzh.javaeye.com/blog/425458



          天蒼蒼,野茫茫,風吹草底見牛羊

          Feedback

          # 免費SVN svn.jsp1.net  回復  更多評論   

          2012-05-11 09:11 by 張嘎子
          不錯,速度很快

          posts - 80, comments - 24, trackbacks - 0, articles - 32

          Copyright © asdtiang

          asdtiang的博客 PaidMailz
          點擊廣告網賺A(每天4個廣告,每個0.0025美元,一個搜索廣告0.03美元)
          主站蜘蛛池模板: 固镇县| 徐闻县| 多伦县| 金阳县| 玉田县| 鄂伦春自治旗| 宜良县| 九龙城区| 三明市| 乡宁县| 尉犁县| 阳东县| 左权县| 德保县| 无为县| 灵台县| 灌云县| 高州市| 永善县| 安康市| 东乡县| 施甸县| 柏乡县| 余姚市| 岱山县| 西峡县| 安龙县| 贵阳市| 新竹县| 临沧市| 永胜县| 东源县| 自贡市| 团风县| 灵璧县| 连山| 华容县| 高平市| 轮台县| 樟树市| 温泉县|