Gaetan
Gaetan

Reputation: 66

How to Get remoteHost java

I'm trying to get a client's hostname, I use this code:

public String GetHostIP(HttpServletRequest request) throws UnknownHostException {
        String ipAddress = request.getRemoteAddr();
        System.out.println("remoteAddress ipAddress is : " + ipAddress);
        try {
            InetAddress Adress = InetAddress.getByName(ipAddress);
            clientHostName = Adress.getHostName();
            System.out.println("Host name is " + clientHostName);
        } catch (UnknownHostException e) {
            System.out.println(e.getMessage());
        }
        return clientHostName;
    }

My question is, why I have same result whatever the host connected

remoteAddress ipAddress is : 10.8.100.30 Host name is: 10.8.100.30

Thanks !

Upvotes: 1

Views: 975

Answers (1)

Idos
Idos

Reputation: 15310

From the documentation (boldface mine):

public String getHostName()

Gets the host name for this IP address. If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName.

If there is a security manager, its checkConnect method is first called with the hostname and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, it will return the textual representation of the IP address.

This is the relevant documentation of checkConnect.

Upvotes: 2

Related Questions