Getting the IP Address Of A client For a webservice
I am using JAX-WS and I开发者_C百科 am having trouble retrieving the client information that is consuming a webservice. I've found out how to do it with JAX-RPC, and Apache Tomcat Axis, but not with 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.
@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();
}
精彩评论