Reputation: 495
I'm running my application on Windows Server 2008 on an Intranet.
To login the application tries to get the hostname from the request to validate the user. However, sometimes the application returns the IP address instead of the name and some time later, without doing anything the application is able to resolve the name and everything works fine...
This is the code I'm using to get the hostname:
InetAddress inaHost = InetAddress.getByName(request.getRemoteAddr());
String hostname = inaHost.getHostName();
System.out.println("[[ Hostname = " + hostname + " ]]");
Is this because of the Intranet configuration (DNS!?), or is something wrong with my code, or witchcraft or something?
Upvotes: 5
Views: 41269
Reputation: 105
sometimes the application returns the IP address instead of the name
As referenced from this SO answer:
The issue could be since request.getRemoteHost() does a reverse DNS lookup instead of taking it from the HTTP headers; if it fails to look up the DNS information using the IP, it returns the IP address as a String.
Upvotes: 0
Reputation: 18923
You will need to use the following function to get the remote address/hostname:
request.getRemoteHost();
Upvotes: 3
Reputation: 5247
First try
System.out.println("Host = " + request.getServerName());
System.out.println("Port = " + request.getServerPort());
if doesnt work
hostName == null;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
{
while (interfaces.hasMoreElements()) {
NetworkInterface nic = interfaces.nextElement();
Enumeration<InetAddress> addresses = nic.getInetAddresses();
while (hostName == null && addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLoopbackAddress()) {
hostName = address.getHostName();
}
}
}
}
Upvotes: 6