欢迎光临
我们一直在努力

从Web Services中访问服务器变量-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

在新闻组中最经常被问到的问题就是“如何从一个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内容。
(转)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 从Web Services中访问服务器变量-.NET教程,Web Service开发
分享到: 更多 (0)