2012-02-15 8 views

risposta

49

Prova questo:

InetAddress address = InetAddress.getByName(new URL(urlString).getHost()); 

Per ottenere l'IP grezzo:

String ip = address.getHostAddress(); 
+1

Grazie che ha funzionato, anche se restituisce un/Nome Indirizzo IP. Se voglio usare l'indirizzo IP per un socket devo usare il '/' come delimitatore per estrarre solo l'indirizzo IP o funzionerebbe così com'è? – user1205853

+2

chiama semplicemente address.getHostAddress() sull'oggetto InetAddess per ottenere una versione stringa dell'IP. O meglio, crea il socket direttamente con l'oggetto InetAddress. – brettw

+0

@brettw: ho modificato la mia risposta nello stesso momento in cui hai commentato. –

10

È necessario dare hostname-getByName() metodo e restituisce

l'indirizzo IP di un host , dato il nome dell'host.

URL url = new URL("http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif"); 
System.out.println(url.getHost()); 
InetAddress address = InetAddress.getByName(url.getHost()); 
System.out.println(address.toString()); 

uscita = www.engineering.uiowa.edu/128.255.17.182

per ottenere il IP address

String temp = address.toString(); 
String IP = temp.substring(temp.indexOf("/")+1,temp.length());