java如何获取用户真实的ip
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何获取用户真实的ip相关的知识,希望对你有一定的参考价值。
百度上有两种方法
第一种
public static String getRemortIP(HttpServletRequest request)
if (request.getHeader("x-forwarded-for") == null)
return request.getRemoteAddr();
return request.getHeader("x-forwarded-for");
第二种
public static String getIpAddr(HttpServletRequest request)
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.getRemoteAddr();
if (ip.equals("0:0:0:0:0:0:0:1"))
ip = "本地";
return ip;
我想问一下是不是两种都可以。请说明可以的方式的具体这样写的原因。
还有网上还有一些说如果有代理,就怎样怎样,是什么意思?
1、如果服务器如果没有采用反向代理,而且客户端没有用正向代理的话,那么可以获取客户端的真实IP地址request.getRemoteAddr()
2、如果服务器如果没有采用反向代理,而且客户端有用正向代理的话,那么通过request.getRemoteAddr()获取客户端的IP地址是客户端 的代理服务器的地址,并不是客户端的真实地址,
3、如果客户端使用的是多层代理的话,服务器获得的客户端地址是客户端的最外围代理服务器的地址如果服务器如果采用反向代理服务器,不管客户端采用的是何种方式访问服务器。
//获得客户端真实IP地址的方法一:public String getRemortIP(HttpServletRequest request)
if (request.getHeader("x-forwarded-for") == null)
return request.getRemoteAddr();
return request.getHeader("x-forwarded-for");
//获得客户端真实IP地址的方法二:
public String getIpAddr(HttpServletRequest request)
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.getRemoteAddr();
return ip;
参考技术A public String IP() throws IOException
String IP=null;
URL url = new URL("http://iframe.ip138.com/ic.asp");
URLConnection conn = url.openConnection();
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,
"GB2312"));
String line = null;
while ((line = br.readLine()) != null)
if (line.contains("您的IP是"))
// System.out.println(line);
int start = line.indexOf('[') + 1;
int end = line.indexOf(']');
IP=line.substring(start, end);
br.close();
return IP;
抓取网路使用的IP追问
什么来的?
追答模拟上网讯号并抓取该网页所显示IP
追问这个问题和我问的有关系吗?
追答= ="
你不是要知道IP??
这个是抓到你真的使用来上网的IP,除非你使用的代理是直接使用模拟网路卡不然这个都会直接抓取到真实IP
Java中使用HttpRequest获取用户真实IP地址端口
1 import javax.servlet.http.HttpServletRequest; 2 3 /** 4 * 自定义访问对象工具类 5 * 6 * 获取对象的IP地址等信息 7 * @author X-rapido 8 * 9 */ 10 public class CusAccessObjectUtil { 11 12 /** 13 * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, 14 * 16 * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? 17 * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 18 * 19 * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 20 * 192.168.1.100 21 * 22 * 用户真实IP为: 192.168.1.110 23 * 24 * @param request 25 * @return 26 */ 27 public static String getIpAddress(HttpServletRequest request) { 28 String ip = request.getHeader("x-forwarded-for"); 29 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 30 ip = request.getHeader("Proxy-Client-IP"); 31 } 32 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 33 ip = request.getHeader("WL-Proxy-Client-IP"); 34 } 35 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 36 ip = request.getHeader("HTTP_CLIENT_IP"); 37 } 38 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 39 ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 40 } 41 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 42 ip = request.getRemoteAddr(); 43 } 44 return ip; 45 } 46 47 }
/**
*获取端口
*/
1 public void doGet(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String uri = request.getRequestURI();//返回请求行中的资源名称
4 String url = request.getRequestURL().toString();//获得客户端发送请求的完整url
5 String ip = request.getRemoteAddr();//返回发出请求的IP地址
6 String params = request.getQueryString();//返回请求行中的参数部分
7 String host=request.getRemoteHost();//返回发出请求的客户机的主机名
8 int port =request.getRemotePort();//返回发出请求的客户机的端口号。
9 System.out.println(ip);
10 System.out.println(url);
11 System.out.println(uri);
12 System.out.println(params);
13 System.out.println(host);
14 System.out.println(port);
15 }
以上是关于java如何获取用户真实的ip的主要内容,如果未能解决你的问题,请参考以下文章