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?