Java开发WebService获取客户端IP地址

来自:jiaguwen123   记录下,以后备用

Java开发的WebService获取客户端IP地址

 

 

 1:基于jax-ws开发的webservice获取客户端ip的方法【在代理类XXXDelegate添加如下代码】

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.基于axis发布的webservice获取客户端ip的方法

 

/**

  * 获取客户端IP地址,如调用方的IP,以便检查权限。

  * 适用于axis发布的webservice

  * @return

  */

  public String getClientIpAxis() {

  MessageContext mc = null;

  HttpServletRequest request = null;

  try {

  mc = MessageContext.getCurrentMessageContext();

  if (mc == null)

  throw new Exception(“无法获取到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.基于xfire发布的webservice获取客户端ip的方法

 

 /**

  * 获取客户端IP地址

  * 适用于xfire发布的webservice

  * @return

  */

  public String getClientIpXfire() {

  String ip = “”;

  try {

  HttpServletRequest request = XFireServletController.getRequest();

  ip = request.getRemoteAddr();

  } catch (Exception e) {

  System.out.println(“无法获取HttpServletRequest.”);

  e.printStackTrace();

  }

  return ip;

  }
 

4:场景: 客户端--->bs服务器--->ws服务器 (未验证

GetSOAPWebModule.Request.RemoteAddr;  
// 通过代理,获取真实的ip
if(Context.Request.ServerVariables["HTTP_VIA"]!=null)
{ 
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 
}
else
{ 
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); 
}
// 或客户端获取再通过方法传递给服务器
 

 

 

 

你可能感兴趣的:(java,xml,webservice,servlet)