Accessing server variables from Web services

Source: Internet
Author: User
Tags variables servervariables web services
services|web| Variable | access | The most frequently asked question about servers in newsgroups is "How do you get the IP address of a client's browser from within a Web services (Web service)?" "The answer to this question is very simple. The context class within the System.Web.Services namespace represents the contexts of the Web service. In other words, it references different objects from within a running Web service. such as response (response), request (requests) and session objects, and debugging activation on the service.

In this article we use a very basic example to describe two things:

1, access to the client's browser IP address
2, get all the Web server variables

The source code is as follows, it is easy to understand:

<%@ 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" server variables as HTML
[WebMethod]
Public String Allhttpvars ()
{
Instantiate a collection that would hold the
Key-value Collection of server variables
NameValueCollection Servervars;
String returnvalue = "";
Servervars = Context.Request.ServerVariables;
Retrieve 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 + +)
{
returnvalue+= "<b>" + arvars[x] + "&LT;/B&GT;:";
returnvalue+= Servervars[arvars[x]] + "<br>";
}
Return returnvalue;
}
}

Http://www.dotnet101.com/articles/demo/art033_servervars.asmx for code demonstrations. Note: The second method Allhttpvars () returns the HTML content.
Go


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.