【问题标题】:How to get the I.P. Address of client that is hitting your webservice? [closed]如何获得IP正在访问您的 Web 服务的客户端的地址? [关闭]
【发布时间】:2014-09-16 21:22:49
【问题描述】:

标题几乎说明了一切,我使用 JAX-RS 和 Jersey 用 Ja​​va 编写了一个 REST Web 服务,我想提取 IP。在我的代码中点击它的客户端的地址。如果我有这样的课程:

@Path("/service")
public class Service {

    @GET
    public void doAction () {

        // ...

    }

}

我从那里做什么?

附:我不是寻求调试帮助,我是在询问如何检索连接到我的 Web 服务的客户端的 IP 地址。 @ANyarThar 提供了一个很好的答案。

【问题讨论】:

  • 你能在你的 REST URL 中强制设置 ipAddress 参数吗?
  • 你的意思是要求客户端明确输入他们的IP地址?不……遗憾的是没有
  • 不确定 Jersey,但在 Spring MVC 中,您可以将 HTTP 请求作为方法参数传递。

标签: java web-services rest jersey


【解决方案1】:

@Context HttpServletRequest 如何在您的方法中作为参数,

@GET
public void doAction (@Context HttpServletRequest req) {
    //then get ip address
    String ipAddress = req.getRemoteHost();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多