2013-07-26 55 views
3

到目前爲止,我做到以下幾點:如何獲得我的計算機的IP(Java/Selenium)?

driver.get("http://www.whatismyip.com/"); 
String myIP = driver.findElement(By.id("greenip")).getText(); 
System.out.println(myIP); 

有沒有什麼更好的辦法?

(PS:我想this solution但NetworkInterface的給多個IP地址,並在另一方面InetAddress.getLocalHost().getHostAddress()給出錯誤的IP。)

+0

尋找你的IP互聯網上你可能想要查看http://stackoverflow.com/questions/8083479/java-getting-my-ip-address或http://stackoverflow.com/questions/9481865/how-to-get-ip-address-我們自己的系統使用java –

+1

你是什麼意思的更好?更快,更可靠?最好的方式不會涉及硒 –

+0

謝謝你的答覆。我只用硒因爲我不知道其他方式。有沒有其他方法? –

回答

1

的事情是有一個像我的IP沒有這樣的事情。你的機器可以有多個接口,每個接口可以有多個IP。所以我真的不認爲你想進入它。您所能做的就是嘗試使用Web API來獲取您的外部IP地址。這是在Java代碼實際上只是這些網站的幫助

public static String getIpAddress() 
    { 
    URL myIP; 
    try { 
     myIP = new URL("http://api.externalip.net/ip/"); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(myIP.openStream()) 
       ); 
     return in.readLine(); 
    } catch (Exception e) 
    { 
     try 
     { 
      myIP = new URL("http://myip.dnsomatic.com/"); 

      BufferedReader in = new BufferedReader(
        new InputStreamReader(myIP.openStream()) 
        ); 
      return in.readLine(); 
     } catch (Exception e1) 
     { 
      try { 
       myIP = new URL("http://icanhazip.com/"); 

       BufferedReader in = new BufferedReader(
         new InputStreamReader(myIP.openStream()) 
         ); 
       return in.readLine(); 
      } catch (Exception e2) { 
       e2.printStackTrace(); 
      } 
     } 
    } 

return null; 
} 
現在有了這個

,你可以從互聯網上獲得你的IP,但是又是你的外部IP

相關問題