Java-developed WebService to obtain client IP address

  1. The method of obtaining the client ip based on the webservice developed by jax-ws [add the following code to the proxy class XXXDelegate]: write
import javax.annotation.Resource;

  import javax.servlet.http.HttpServletRequest;

  import javax.xml.ws.WebServiceContext;

  import javax.xml.ws.handler.MessageContext;

  @Resource

  private WebServiceContext wsContext;

  private String getClientInfo(){

  MessageContext mc = wsContext.getMessageContext();

  HttpServletRequest request = (HttpServletRequest)(mc.get(MessageContext.SERVLET_REQUEST));

  String remortAddress = request.getRemoteAddr();

  return (remortAddress);

  }

 

2. The method of obtaining the client ip based on the webservice published by axis is written

/**

  * Get the client IP address, such as the caller's IP, in order to check permissions.

  * Applicable to webservice published by axis

  * @return

  */

  public String getClientIpAxis() {

  MessageContext mc = null;

  HttpServletRequest request = null;

  try {

  mc = MessageContext.getCurrentMessageContext();

  if (mc == null)

  throw new Exception(" Failed to get MessageContext");

  request = (HttpServletRequest) mc

  .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

  System.out.println("remote ip: " + request.getRemoteAddr());

  } catch (Exception e) {

  System.out .println(e.getMessage());

  e.printStackTrace();

  }

  return request.getRemoteAddr();

  }

 

3. The method of obtaining the client ip based on the webservice published by xfire is written
/**

  * Get client IP address

  * Applicable to webservice published by xfire

  * @return

  */

  public String getClientIpXfire() {

  String ip = "";

  try {

  HttpServletRequest request = XFireServletController.getRequest();

  ip = request.getRemoteAddr( );

  } catch (Exception e) {

  System.out.println("Unable to get HttpServletRequest.");

  e.printStackTrace();

  }

  return ip;

  }

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987786&siteId=291194637