Saturday, March 24, 2012

Server Variables

Hi, how do I tap into the information of server variables such as server Name etc.? Thank you.HttpServerUtility Class has a property MachineName
Pardon me, Martin, but I am very new to web programming. HttpServerUtility is under the System.web namespace? And any samples on how to use it? Thanks, MArtin.
Try this:


Request.ServerVariables["SERVER_NAME"].ToString()

Hi Azam, thanks for your reply. I would also like to know how to display my current WAN IP using ASP.NET? I tried using a httpwebrequest to www.whatismyip.org and when I handled the httpwebresponse, I only got back the IP of my webhoster instead of my current computer (client)'s WAN IP (given to me by my ISP). Thanks, Azam.
Not sure what you want to do exactly. I had noticed that you had posted to a previous thread in which the solution that azamsharp has suggested was mentioned. That is a better solution if all you want is the server name. Request.ServerVariables also holds a whole heap of other info. What exactly are you looking for ?

Cheers
Martin
Hi Martin. I would like to do what www.whatismyip.org is trying to do. This page will tell you your current WAN IP. However, if I were to pass in a HttpWebRequest from my website to this whatismyip.org, the httpwebresponse will return the IP of the web hosting company instead of my current browser's WAN IP. Sorry for not making myself clear... :)
I think you are after this -

Request.ServerVariables["LOCAL_ADDR"].ToString()

Hi Martin, which namespace do I import in order to use your code provided in the previous thread? Thanks a lot, Martin.
System.Web
Hi,
I have imported the namespace but when I do the following code, it failed. please advise.

Dim Temp as String = Convert.Tostring(Request.ServerVariables["LOCAL_ADDR"])
When you say failed, how exactly it failed. It always helps to explain a little more, rather than simply saying its not working or it failed. Please post the error message
I'm guessing that it failed because you are trying to convert a string to a string. Convert's ToString method does not have a template that takes a string input. ServerVariables returns a string.

And, I agree with Kumar, more detail next time please.

Thanks
Martin
Hi guys, sorry for the delay. Anyway, the error message shown is "Compiler Error Message: BC30203: Identifier expected". Thank you. The full code for the function is

Sub btnServer_Click(sender As Object, e As EventArgs)
Dim Temp as String = Convert.Tostring(Request.ServerVariables["LOCAL_ADDR"])
lblservername.text = temp
End Sub
For Visual Basic it should be:
Request.ServerVariables.Item("LOCAL_ADDR")

0 comments:

Post a Comment