扫二维码关注kmpro
KMPRO
站内搜索
 位置:首页>知识管理实施>IT实施阶段>

Accessing Server Variables From Within Web Services 来源:   时间:2002-09-12 14:50   作者:AMTeam.org

Accessing Server Variables From Within Web Services

 

Manohar Kamath

April 2, 2001

Level: Beginner

One of the most often asked question in newsgroups is "How do I get the IP address of the client browser within a web service?" The answer is very simple. The Context class within the system.web.services namespace represents the context of the web service. In other words, it has reference to various objects from within a running web service -- things like Response, Request and Session objects, and also such information as if the debugging is enabled on the service.

In this article, we will take a look at a very basic example that describes two things

Retrieve the IP address of the client browser

Retrieve all the web server variables

The source code is self explanatory, and is not supposed to be an introduction to web services (for such articles, refer to Web Services. Part I: The Basics).

<%@ Webservice Language="C#" class="httpvars" %>

using System;

using System.Collections;

using System.Web.Services;

public class httpvars : WebService

{
   
// This method returns the IP address of the client
   
[WebMethod]
   
public String ipAddress ()
   
{
       
// The Context object contains reference to Request object
       
return Context.Request.ServerVariables["REMOTE_ADDR"];
   
}

 // This method returns the all the server variables as HTML
   
[WebMethod]
   
public String allHttpVars ()
   
{
       
// Instantiate a collection that will hold the
       
// key-value collection of server variables
       
NameValueCollection serverVars;
       
String returnValue = "";

serverVars = Context.Request.ServerVariables;

 // Retrieve all the Keys from server variables collection
      
  // as a string array
       
String[] arVars = serverVars.AllKeys;

 // Loop through the keys array and obtain the
       
// values corresponding to the individual keys
       
for (int x = 0; x < arVars.Length; x++)
       
{
           
returnValue+= "<b>" + arVars[x] + "</b>: ";
           
returnValue+= serverVars[arVars[x]] + "<br>";
       
}
       
return returnValue;
   
}

}

Demo Code

The second method allHttpVars() returns HTML.

关键词:
相关文章
分享到:
2010-2012 北京深蓝海域信息科技有限公司
Darkblue Sea Software Co., Ltd. all rights reserved
京ICP备13004508号-1
地址:北京市昌平区北清路1号珠江摩尔国际中心6号楼1单元907单位
电话:010-89146616/82969378
传真:010-82969378
公司介绍 | 人才招募 | 网站地图 | 联系我们 | 企业资质