Accessing Server Variables From Within Web Services
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.- 1石家庄泛普OA软件管理门户登录
- 2知识经济时代的知识、竞争与制度演化
- 3协同之惑
- 4出版社行业如何做好信息化建设的思考
- 5用SLA保证Web服务
- 6石家庄OA信息化的基本XML和RDF技术(四):问题跟踪程序模式
- 7泛普OA个性化门户主要提供了基于用户的门户个性化流程
- 8IBM WebSphere以最快速度部署开放的Web服务
- 9石家庄OA信息化——信息化的奢侈品?<br><div align=right>——夏敬华专访&l
- 10OA办公系统可以帮助企业摆脱束缚
- 11Web服务设计师,第2部分:动态电子商务模式
- 12包装你知道的一切
- 13[编译] 石家庄OA信息化测度:目标、过程及方法(夏敬华译)
- 14从九点连线谈创新及对石家庄OA信息化的再思考(by AMT 夏敬华)
- 15[评论] 比尔.盖茨论石家庄OA信息化
- 16如何使用Visual Studio .NET和Office XP创建和部署XML Web Service
- 17实用工具:IT员工的培训补偿政策示例
- 18管理结构式Web服务元数据
- 19初探石家庄OA信息化
- 20网络服务热火如潮 前景扑朔迷离
- 21Ask the DotNetJunkies: Consuming Remote Web Services in ASP.
- 22[原创]OA选择首先要清晰概念
- 23两种对立标准联姻 微软IBM规范Web服务
- 24BEA荣获最佳web服务产品奖
- 25如何利用CICADA知识搜索引擎提升知识获取能力
- 26A Web Services Primer
- 27Sun等公布Web服务协调语言“WSCI”
- 28柴油机故障诊断专家系统知识库设计
- 29泛普软件石家庄OA信息化系统实施9大推进步骤
- 30分析家:安全仍是Web服务普及最大障碍
成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼