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

          java 根據(jù)IP獲取所在地

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

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

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

          結(jié)果只取了主數(shù)據(jù)。

          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很不穩(wěn)定,容易獲取不到,我測試是這樣的,最好的方法還是有自己的IP數(shù)據(jù)庫。
          純真IP數(shù)據(jù)庫剛好可以用上。
          具體參考:http://javagp.group.javaeye.com/group/topic/11866
          在他的博客上有源碼下載,直接用就可以了





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



          天蒼蒼,野茫茫,風(fēng)吹草底見牛羊

          Feedback

          # 免費SVN svn.jsp1.net  回復(fù)  更多評論   

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

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

          Copyright © asdtiang

          asdtiang的博客 PaidMailz
          點擊廣告網(wǎng)賺A(每天4個廣告,每個0.0025美元,一個搜索廣告0.03美元)
          主站蜘蛛池模板: 六盘水市| 盖州市| 太保市| 和顺县| 林甸县| 从江县| 平阴县| 奈曼旗| 乐安县| 清水河县| 泗阳县| 都安| 西和县| 潜江市| 保定市| 若尔盖县| 威宁| 澄迈县| 通化市| 墨江| 夏津县| 青海省| 东兰县| 青铜峡市| 龙井市| 江陵县| 巴中市| 满洲里市| 鹤岗市| 济宁市| 九江县| 张家界市| 会泽县| 叙永县| 华容县| 来凤县| 顺昌县| 莆田市| 永和县| 宣威市| 万载县|