获取用户的外部IP

URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=myip");


  1. public static String GetNetIp()  
  2.     {  
  3.         URL infoUrl = null;  
  4.         InputStream inStream = null;  
  5.         try  
  6.         {  
  7.             //http://iframe.ip138.com/ic.asp  
  8.             //infoUrl = new URL("http://city.ip138.com/city0.asp");  
  9.             infoUrl = new URL("http://iframe.ip138.com/ic.asp");  
  10.             URLConnection connection = infoUrl.openConnection();  
  11.             HttpURLConnection httpConnection = (HttpURLConnection)connection;  
  12.             int responseCode = httpConnection.getResponseCode();  
  13.             if(responseCode == HttpURLConnection.HTTP_OK)  
  14.             {   
  15.                 inStream = httpConnection.getInputStream();   
  16.                 BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));  
  17.                 StringBuilder strber = new StringBuilder();  
  18.                 String line = null;  
  19.                 while ((line = reader.readLine()) != null)   
  20.                     strber.append(line + "\n");  
  21.                 inStream.close();  
  22.                 //从反馈的结果中提取出IP地址  
  23.                 int start = strber.indexOf("[");  
  24.                 int end = strber.indexOf("]", start + 1);  
  25.                 line = strber.substring(start + 1, end);  
  26.                 return line;   
  27.             }  
  28.         }  
  29.         catch(MalformedURLException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.         catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         return null;  
  36.     } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值