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使用WSDL部署Web服务,第2部分:简单对象访问协议(SOAP)
- 3SOAP技术与B2B应用集成--SOAP消息中的类型/值的编序方法和示例
- 4The DECOR Project:Workflow-Embedded Organizational Memory Ac
- 5Using ASP.NET/WebServices For UPS Shipping Quotes
- 6[编译] 石家庄OA信息化测度:目标、过程及方法(夏敬华译)
- 7Web服务 按需服务——Sun ONE为企业建立服务驱动型网络
- 8Web服务内幕,第7部分:WSFL和递归组合
- 9固化组织知识
- 10Web服务的(革)创新,第4部分
- 11将Web服务用于电子交易的单点登录
- 12利用FrontPage来使用XML Web Service
- 13“低价狂人”栾润峰:有理想的冒险才值得
- 14微软展示新版互联网服务MSN 8.0
- 15Web服务内幕,第1部分:我们已走了多远?
- 16对某集团公司协同办公系统未来3-5年的IT规划建设蓝图
- 17泛普软件石家庄OA信息化系统实施9大推进步骤
- 18解读德鲁克
- 19由 80/20 法則突破「知識分享」的心防
- 20软件里面的思想黑马:金和董事长栾润峰
- 21OA办公系统软件信息传递的安全解决方案
- 22BEA荣获最佳web服务产品奖
- 23OA办公系统的信息发布与管理门户介绍
- 24图书馆行业信息化建设现状
- 25架构Web Service:什么是Web服务?
- 26无SOAP的Web服务,第一部分
- 27初探石家庄OA信息化
- 28一波“三折”:我的OA选型经历(下)
- 29Sun等公布Web服务协调语言“WSCI”
- 30石家庄OA信息化创造竞争优势
成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼