Using Server Variables

Across the Web, active server pages make extensive use of the Request object’s ServerVariables collection to determine specifics about a request, such as the URL from which a user linked to the site (the referring site), the request type (get or post), and the request’s protocol type (such as HTTP). The active server page in Listing 5.16, ServerVariables.asp, displays a listing of the entries the ServerVariable container stores as well as each entry’s value.

Listing 5.16 ServerVariables.asp

start example
<html> <head>    <title>Server Variables</title> <head> <body>   <center><h1>Server Variable Entries</h1></center>   <%          Dim Entry          For Each Entry in Request.ServerVariables          Response.Write(Entry & " "  & Request.ServerVariables(Entry) & "<br>")          Next    %> <body> <html>
end example

When a user visits the active server page, the script will display output similar to that shown in Figure 5.10.

click to expand
Figure 5.10: Displaying entries within the ServerVariable collection

Depending on the processing a web service performs, there may be times when your code needs to know specifics about the request. The following ServerInformation web service provides two methods that return ServerVariable entries. The first returns a string array that contains settings for five common entries. The second lets the user query a specific setting:

String Demo() String QueryEntry(ByVal Entry As String)

To create the ServerInformation web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name ServerInformation. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.17.

Listing 5.17 ServerInformation.asmx.vb

start example
<WebMethod()> Public Function Demo() As String()     Dim Result(5) As String     Dim Entry As String     Result(0) = Context.Request.ServerVariables("SERVER_SOFTWARE")     Result(1) = Context.Request.ServerVariables("SERVER_PORT")     Result(2) = Context.Request.ServerVariables("SERVER_PROTOCOL")     Result(3) = Context.Request.ServerVariables("REQUEST_METHOD")     Result(4) = Context.Request.ServerVariables("SCRIPT_NAME")     Demo = Result End Function <WebMethod()> Public Function QueryEntry(ByVal Entry As String)_ Ä   As String     Dim Result As String     Result = Context.Request.ServerVariables(Entry)     If Result Is Nothing Then         QueryEntry = "Not found"     Else         QueryEntry = Result     End If End Function 
end example

The following C# program, ShowServerVariables.cs, uses the ServerVariables web server to let the user request specifics about the ServerVariable. As shown in Figure 5.11, the program displays a form with two buttons. When the user selects the Demo button, the program displays the values for several common entries. If the user enters a specific ServerVariables entry and then selects the Query button, the program will display the value of the corresponding entry within the web service’s ServerVariable.

click to expand
Figure 5.11: Querying a web service’s ServerVariable object

To create the ShowServerVariables.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type ShowServerVariables. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

  3. Using the Toolbox, drag and drop the text box and button previously shown in Figure 5.11 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/ServerInformation/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.18.

Listing 5.18 ShowServerVariables.cs

start example
private void button1_Click(object sender, System.EventArgs e) {     localhost.Service1 WS = new localhost.Service1();     string [] Entries = new string[5];     try     {         Entries = WS.Demo();         foreach (string entry in Entries)         {             textBox1.Text += entry + "\r\n";         }     }     catch (Exception ex)     {         textBox1.Text = ex.Message;     } } private void button2_Click(object sender, System.EventArgs e) {     localhost.Service1 WS = new localhost.Service1();     try     {         if (textBox2.Text.Length > 0)         textBox3.Text = WS.QueryEntry(textBox2.Text);     }     catch (Exception ex)     {         textBox3.Text = ex.Message;     } }
end example

As you can see, when the user selects the Demo button, the code creates an object that corresponds to the web service and then calls the service’s Demo method, assigning the result to an array string. The code then uses a foreach loop to cycle through the array entries, assigning each entry to a text box. Likewise, when the user selects the Query button, the code calls the service’s QueryEntry method, assigning the string result to a text box. If the entry that the user specifies is not found, the method will display an entry so stating.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net