java如何获取用户真实的ip 5
百度上有两种方法第一种publicstaticStringgetRemortIP(HttpServletRequestrequest){if(request.getHea...
百度上有两种方法
第一种
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;
}
我想问一下是不是两种都可以。请说明可以的方式的具体这样写的原因。
还有网上还有一些说如果有代理,就怎样怎样,是什么意思? 展开
第一种
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;
}
我想问一下是不是两种都可以。请说明可以的方式的具体这样写的原因。
还有网上还有一些说如果有代理,就怎样怎样,是什么意思? 展开
2个回答
推荐于2016-01-13
展开全部
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;
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
展开全部
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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询