今天在学习request的相关方法时遇到一个问题
String remoteAddr = request.getRemoteAddr();
System.out.println("remoteAddr ::: "+remoteAddr);
String remoteHost = request.getRemoteHost();
System.out.println("remoteHost ::: "+remoteHost);
控制台输出结果:
remoteAddr ::: 0:0:0:0:0:0:0:1
remoteHost ::: 0:0:0:0:0:0:0:1
为什么会显示IPV6的地址呢?而且还是0:0:0:0:0:0:0:1
经过研究后发现,通过getRemoteAddr()和getRemoteHost()时,客户机和服务器再同一台电脑导致的
我的请求路径为:
<a href="http://localhost:8080/user?username=Jhon&password=10">
将请求路径中的localhost替换为127.0.0.1
<a href="http://127.0.0.1:8080/user?username=Jhon&password=10">
再次请求观察获取到的IP地址
remoteAddr ::: 127.0.0.1
remoteHost ::: 127.0.0.1
显示称为了本机的IPV4地址!
本文探讨了在使用request对象获取客户端IP地址时遇到的问题,即为何返回的是IPv6地址而非预期的IPv4地址,并通过修改请求路径成功获取到了127.0.0.1这一标准的本地IPv4地址。
1207

被折叠的 条评论
为什么被折叠?



