3

I got a problem when trying to get IP from HttpServletRequest,Please see my coding first:

String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip; 

My problem is if opened application with following URL (My PC's URL is 18.111 and the server deployed on localhost) "https://192.168.18.111:8443/test/main.html",I could get right URL with above coding,But if opened with "https://localhost:8443/test/main.html",It would return something like "0.1.0.1...." with above function,Why this function didn't work for "localhost" or does anybody know if have any better way to get IP from HttpServletRequest?

Chailie
  • 451
  • 2
  • 11
  • 24
  • 0.1.0.1? wow, no 127.0.0.1? you have IPv6 enabled in your machine? what OS have? try with https://127.0.0.1:8443/test/main.html – ggrandes Jan 06 '13 at 08:33
  • My OS is ubuntu and i am not sure if i have enabled IPV6 in my machine – Chailie Jan 07 '13 at 02:33

2 Answers2

6

Your method's result is absolutely correct. I assume the number that you're getting is 0:0:0:0:0:0:0:1. It's valid form of loopback address. But it's just localhost in IPv6 format. IPv4 address of localhost is 127.0.0.1 and IPv6 address of localhost is 0:0:0:0:0:0:0:1.

The thing's that the URL https://localhost:8443/test/main.html matches by default both versions of IP protocol. Apparently your browser chooses to use IPv6.

For local testing try using literal address 127.0.0.1 instead of name localhost. Or you can make have only IPv4 address in your DNS settings.

kapandron
  • 3,546
  • 2
  • 25
  • 38
  • OK,Get it,But for this"you can make have only IPv4 address in your DNS settings." How to do the setting,In browser? – Chailie Jan 07 '13 at 02:32
1

Why not trying

request.getRemoteAddr()

only? Do you really need the originating IP address (the one provided by "X-Forwarded-For")? I'm guessing that sometimes it's not useful to see some dumb LAN address instead of an addressable resource - provided by the upper method.

Later edit:

Have a look at this question: Finding user ip address which seems to be fixed.

Community
  • 1
  • 1
Ariel Chelsău
  • 959
  • 3
  • 9
  • 20
  • `getRemoteAddress()` doesn't provide you IP address of client, if network has intermediate nodes. This's especially true for proxies and load-balancers. – kapandron Jan 06 '13 at 15:34
  • You're right, this is why I added an edit to my answer. The OP can check the correct answer from there. – Ariel Chelsău Jan 06 '13 at 16:07