监理公司管理系统 | 工程企业管理系统 | OA系统 | ERP系统 | 造价咨询管理系统 | 工程设计管理系统 | 签约案例 | 购买价格 | 在线试用 | 手机APP | 产品资料
X 关闭

从Web Services中访问服务器变量

申请免费试用、咨询电话:400-8352-114

AMTeam.org

从Web Services中访问服务器变量

在新闻组中最经常被问到的问题就是“如何从一个web Services(Web服务)内部获取客户浏览器的IP地址?” 这个问题的答案非常简单。system.web.services名称空间内部的Context类代表了web服务的上下文。换句话说,它从一个正在运行的web服务内部对不同的对象进行引用。比如Response(响应)、Request(请求)和Session对象,以及在服务上调试是否激活之类的信息。

本文我们用一个非常基本的例子来描述两件事:

1、取得客户浏览器的IP地址

2、取得所有的web 服务器变量

源代码如下,很容易理解:

<%@ 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;

}

}

http://www.dotnet101.com/articles/demo/art033_servervars.asmx进行代码演示。注意:第二个方法allHttpVars()返回HTML内容。

发布:2007-03-25 13:22    编辑:泛普软件 · xiaona    [打印此页]    [关闭]