【问题标题】:Host IP Address 0:0:0:0:0:0:0:1 on ServletServlet 上的主机 IP 地址 0:0:0:0:0:0:0:1
【发布时间】:2012-05-10 08:11:10
【问题描述】:

我正在尝试使用以下方式获取用户主机、IP 和端口:

request.getRemoteHost()
request.getRemoteAddr()
request.getRemotePort()

但我得到了:

主机: 0:0:0:0:0:0:0:1
IP: 地址空
端口: 51111

如何获取正确的 IP 地址,注意我们的网络 IP 以 10.100.x.x 开头

非常感谢。

【问题讨论】:

    标签: java jsp servlets network-programming ip


    【解决方案1】:

    如果您在请求 URI 中使用 localhost,则 IPv6 为 0:0:0:0:0:0:0:1。不要使用localhost,而是在您的请求URI 中使用您的IPv4 来获取正确 IP。

    【讨论】:

    • 谢谢这是问题所在,我正在从本地主机进行测试,但是我为 request.getRemoteHost() request.getRemoteAddr() 获得了相同的 IP,所以我如何获得 Netbios 的名称电脑?
    • 好的,我搞定了:InetAddress inetAddress = InetAddress.getByName(request.getRemoteHost());字符串计算机名 = inetAddress.getHostName(); if (computerName.equalsIgnoreCase("localhost")) { computerName = InetAddress.getLocalHost() .getCanonicalHostName(); }
    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2013-07-15
    • 2018-09-26
    • 2016-01-25
    相关资源
    最近更新 更多