Obtain client ip address from web service

I have developed a webservice in java.

I have a single application which needs to be concurrently accessed by multiple clients. Therefore I need to know which client has made each request. Is there a way to obtain the client ip address from the soap response?

Who is Participating?
Inject a WebServiceContext instance into your WebService class, and use it in each WebMethod to get the current request's MessageContext Map. Get the SERVLET_REQUEST item from that, cast as an HttpServletRequest. From that HttpServletRequest, you can invoke getRemoteAddr() to get the client's IP.

public class MyService{

WebServiceContext wsCtxt;

public void myWebMethod(){

MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIP = req.getRemoteAddr();

The answer from petiex above assume some things about the SOAP library you are using and that you are using HTTP as the transport. Let me first ask that: what SOAP library are you using and what is the transport?

The answer is essentially correct though: you need to get access to you transport protocol (usually HTTP) and get it from there.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.