码迷,mamicode.com
首页 > Web开发 > 详细

获取http请求的真实IP地址

时间:2017-03-31 16:55:49      阅读:531      评论:0      收藏:0      [点我收藏+]

标签:地址   art   head   exception   http请求   ret   tip   for   start   

  1. /** 
  2.      * 获取http请求的真实IP地址  
  3.      * @param request 
  4.      * @return 
  5.      */  
  6.     // cjianquan 2016/8/2  
  7.     public static String getIPAddr(HttpServletRequest request){  
  8.         if (request == null)  
  9.             return null;  
  10.         String ip = request.getHeader("X-Forwarded-For");  
  11.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))  
  12.             ip = request.getHeader("Proxy-Client-IP");  
  13.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))  
  14.             ip = request.getHeader("WL-Proxy-Client-IP");  
  15.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))  
  16.             ip = request.getHeader("HTTP_CLIENT_IP");  
  17.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))  
  18.             ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
  19.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))  
  20.             ip = request.getRemoteAddr();  
  21.         if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip))  
  22.             try {  
  23.                 ip = InetAddress.getLocalHost().getHostAddress();  
  24.             }  
  25.             catch (UnknownHostException unknownhostexception) {  
  26.             }  
  27.         return ip;  
  28.     }  

获取http请求的真实IP地址

标签:地址   art   head   exception   http请求   ret   tip   for   start   

原文地址:http://www.cnblogs.com/onedayoh/p/6651870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!