服务器端获取webservice客户端IP地址

将下面这些方法放到服务器端就可以

一、基于xfire发布的webservice获取客户端ip的方法

public String getClientIp(){ 
String ip = ""; 
try { 
HttpServletRequest request = XFireServletController.getRequest(); 
ip = request.getRemoteAddr(); 
System.out.println("客户端ip=:"+ip); 
} catch (Exception e) { 
System.out.println("无法获取HttpServletRequest."); 
e.printStackTrace(); 

return ip; 
}

 

二、基于axis发布的webservice获取客户端ip的方法

 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();

  }


三、基于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);

  }

 


import javax.servlet.http.*;
import javax.xml.rpc.server.*;
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants; 
public class BaseService {
  public String getClientIp(){ 
    MessageContext  mc  =  MessageContext.getCurrentContext();
    HttpServletRequest  request  =  (HttpServletRequest)  mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
    System.out.println("remote  ip:  "  +  request.getRemoteAddr());
    return request.getRemoteAddr();
  }
}

你可能感兴趣的:(服务器端获取webservice客户端IP地址)