url to ip
We are trying to convert a url to its ip address. We use the following code.
InetAddress host = InetAddress.getByName(url);
String ip = host.getHostAddress();
The problem is that the conversion succeeds only if the given url has the following form: xxxxxxxxx.gr.
For example, we can convert the helpdesk.mnec.gr but not the helpdesk.mnec.gr/helpdesk/Questions/CreateQuestion.jsp?epixid=300!
Does anybody know how we can find the ip of a url like the second one? (helpdesk.mnec.gr/helpdesk/Questions/CreateQuestion.jsp?epixid=300) Thanks.
[575 byte] By [
marilenia] at [2007-10-2 6:48:44]

> Does anybody know how we can find the ip of a url
> like the second one?
> (helpdesk.mnec.gr/helpdesk/Questions/CreateQuestion.js
> p?epixid=300) Thanks.
Maybe i am wrong, but why not just split the url and use the first part? (just helpdesk.mnec.gr), the host is the same in both cases.
didia at 2007-7-16 13:57:33 >

the URL class has a method called getHost() which will return the host part only which you can pass to InetAddress's getByName() method and finally get the String representation of that host's IP address...
import java.net.*;
public class GetIP {
public static void main(String[]xyz){
try{
URL u = new URL("http://forum.java.sun.com/thread.jspa?threadID=688524");
InetAddress i = InetAddress.getByName(u.getHost());
System.out.println(i.getHostAddress());
}catch(UnknownHostException uhe){
uhe.printStackTrace();
}catch(MalformedURLException mue){
mue.printStackTrace();
}
}
}
and all this is listed in the java.net.* APIs...
have fun...
- MaxxDmg...
- ' He who never sleeps... '