Accessing Server Variables From Within Web Services

2002-9-12 14:50:45【作者】 畅享网 【进入论坛】
本文关键字 理论探讨
广告

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.

如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐
jill.jiang@amteam.org | 021-51096826-112 | 在线联系
吴勇毅 专栏CIO 应向刘邦学管理

而国内不少专家也认为,“七分管理,三分技术”,CIO优良与否,与技术出身有关,更与整体素质有关。

夏敬华的KM专栏[原创]智慧的和谐—知识管理推..

从知识管理的角度来观察执行力体系,我们会发现,知识管理和战略、运营和人员这三个环节之间有着内在紧密的逻辑联系。

KM八爪鱼-萧秋水的专栏[原创]企业知识库2.0

面对经济危机,企业更应该关注知识管理,关注知识库的构建,扩充知识储备,提高企业智商和竞争优势。

前沿论丛2009年第三期——知识管理..

国内中小企业普遍存在管理基础薄弱、规范化程度低、信息化基础差等方面的问题,而知识管理的实施难度甚至要高于ERP的实施,因为简单的从上而下压迫式的推行只能做到知识……