简体   繁体   English

获取客户端的IP地址对于Web服务

[英]Getting the IP Address Of A client For a webservice

I am using JAX-WS and I am having trouble retrieving the client information that is consuming a webservice. 我正在使用JAX-WS,我在检索使用Web服务的客户端信息时遇到问题。 I've found out how to do it with JAX-RPC, and Apache Tomcat Axis, but not with JAX-WS. 我已经找到了如何使用JAX-RPC和Apache Tomcat Axis,但没有使用JAX-WS。 Does anyone have an idea about this? 有没有人对此有所了解?

What about this: 那这个呢:

@WebService
public class MyService {

  @Resource
  WebServiceContext wsContext; 

  /**
   * Web service operation
   */ 
  @WebMethod 
  public String myMethod() { 

    MessageContext mc = wsContext.getMessageContext();
    HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); 
    System.out.println("Client IP = " + req.getRemoteAddr()); 

  }

} 

Or this: 或这个:

@Path("terminal")
public class terminal {
    @Context private javax.servlet.http.HttpServletRequest hsr;
    @GET
    @Path("get_ip")
    @Produces("text/plain")
    public String get_ip()
    {
            return ip = hsr.getRemoteAddr();
    }
}

Taking a huge and appreciated hint from Zayin and Darren's answer/edit, I tried this, and it works too. 从Zayin和Darren的回答/编辑中获取了巨大且受到赞赏的暗示,我尝试了这个,它也有效。

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("ip")
public String sayIP(@Context HttpServletRequest req, @QueryParam("p1") String p1, ...) {
    return req.getRemoteAddr();
}
public String getIp(@Context HttpServletRequest req) {
    return req.getRemoteHost();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM