Isabella
Joined: 15 Jun 2006 Posts: 100
|
Posted: Wed Aug 16, 2006 3:46 am Post subject: |
|
|
Retrieving IP addresses
Getting the plumbing working on this project is simple: each time you make a Web request, the HTTP headers passed from the client to the Web server include information about the request, including your public Web address. That allows the Web server to respond back to the correct address. The challenge is to find a way to retrieve that header information, and return it to a client application. The answer, of course, is to create a Web service that returns the IP address of the request. This task, in fact, is amazingly simple.
The HttpRequest instance that ASP.NET makes available to you from a Web page or a Web service makes it possible to retrieve all the information you need. From within a Web page—actually within a class that inherits from System.Web.UI.Page—you can refer to Me.Request for access to this instance. In a Web service that inherits from System.Web.Services.WebService, you can use Me.Context.Request to get at the same instance. And regardless of the type of server-side code you're writing, be it a page or a service or an arbitrary handler, you can always go directly to the HttpContext.Current.Request. The HttpRequest.ServerVariables property exposes a name/value collection that contains one item for each of the well-known server variables provided by IIS and ASP.NET. Within this collection, you can retrieve the address of the requesting page, and can easily create a method that returns the value on demand.
The HttpRequest.ServerVariables collection contains all sorts of information about the current request, the client browser, the server, and more. In order to see what's in the collection, the simplest solution is to create a Web page that displays the information for you .To investigate this useful collection, create a new Web application, using either Visual Studio .NET 2003 or Visual Studio 2005. In the default page's code, simply add the following procedure:
| Code: | Private Sub ListServerVariables()
For Each key As String In Request.ServerVariables.Keys
Response.Write(String.Format("<b>{0}</b> = {1}<br>", _
key, Request.ServerVariables(key)))
Next
End Sub |
Finally, add code to the Page_Load event handler to call the ListServerVariables procedure.
| Code: |
' [b]In Visual Studio .NET 2003 or Visual Studio 2005[/b]
Dim ip As New GetIP.AddressInfo
MessageBox.Show(ip.GetAddress())
' Or in Visual Studio 2005
MessageBox.Show(My.WebServices.AddressInfo.GetAddress()) |
Hoping it'd help ya
|
|