熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java開發的WebService獲取客戶端IP地址

2022-06-13   來源: Java核心技術 

  基於jaxws開發的webservice獲取客戶端ip的方法【在代理類XXXDelegate添加如下代碼】

  import javaxannotationResource;

  import javaxservlethttpHttpServletRequest;

  import javaxxmlwsWebServiceContext;

  import javaxxmlwshandlerMessageContext;

  @Resource

  private WebServiceContext wsContext;

  private String getClientInfo(){

  MessageContext mc = wsContextgetMessageContext();

  HttpServletRequest request = (HttpServletRequest)(mcget(MessageContextSERVLET_REQUEST));

  String remortAddress = requestgetRemoteAddr();

  return (remortAddress);

  }

  基於axis發布的webservice獲取客戶端ip的方法

  /**

  * 獲取客戶端IP地址如調用方的IP以便檢查權限

  * 適用於axis發布的webservice

  * @return

  */

  public String getClientIpAxis() {

  MessageContext mc = null;

  HttpServletRequest request = null;

  try {

  mc = MessageContextgetCurrentMessageContext();

  if (mc == null)

  throw new Exception(無法獲取到MessageContext);

  request = (HttpServletRequest) mc

  getProperty(HTTPConstantsMC_HTTP_SERVLETREQUEST);

  Systemoutprintln(remote  ip:  + requestgetRemoteAddr());

  } catch (Exception e) {

  Systemoutprintln(egetMessage());

  eprintStackTrace();

  }

  return requestgetRemoteAddr();

  }

  基於xfire發布的webservice獲取客戶端ip的方法

  /**

  * 獲取客戶端IP地址

  * 適用於xfire發布的webservice

  * @return

  */

  public String getClientIpXfire() {

  String ip = ;

  try {

  HttpServletRequest request = XFireServletControllergetRequest();

  ip = requestgetRemoteAddr();

  } catch (Exception e) {

  Systemoutprintln(無法獲取HttpServletRequest);

  eprintStackTrace();

  }

  return ip;

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26204.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.