getting the host name

Hello all,

I'm working on a asp.net application being used internally within our local network.  I need to get the Host Name of the station that is querying the aspx page.  I tried Request.UserHostName but that returns the IP address, identical to Request.UserHostAddress.

Any way to do this?

Thanks - Trevor
LVL 8
trevorhartmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

raterusCommented:
The only reliable way to do this is to do it yourself, and use a reverse lookup.  The System.Net namespace has the tools to do this (I think it's this one anyway).  Sorry, I'm not at work now, so I can't give you the code, but knowing you, you don't need me to find it for you :-)
ZackronCommented:
Another option is you can use the Dns class within the System.Net assembly to attempt to resolve your IP to a hostname.  This of course requires that the DNS server your server is using is aware of the hostnames of your local systems.

Here is an example of some of the code you can use to get a hostname:

Dim myIP As String = "192.168.10.15"
Dim oHostInfo As IPHostEntry = Dns.GetHostByAddress(msIP)
sHostname = oHostInfo.HostName

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DevastatedCommented:
Hiya,

Taken from:- http://www.c-sharpcorner.com/1/get_ip.asp

**content removed by raterus**

Hope this helps.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

DevastatedCommented:
erm its better to infringe on copyrights u say ?


anyhow trevorhartman,

you can use GetHostName with no parameters, this will return the host name of the local machine.

string strLocalHostName = Dns.GetHostName ();

look at the link in my post above for a fuller explanation and example c# code.
trevorhartmanAuthor Commented:
thanks all, i ended up using:


                  string kioskHostIP = Request.UserHostAddress;
                  
                  // do a reverse lookup to figure the HostName using the IP address
                  IPHostEntry hostInfo = Dns.GetHostByAddress(kioskHostIP);
                  string kioskHostName = hostInfo.HostName;
                  Response.Write(kioskHostName + "<br>");

-Trevor
DevastatedCommented:
Heya,

did you try Request.UserHostName ?
DevastatedCommented:
or more apptly put...

Response.Write("The kiosk Host Name is " + Request.UserHostAddress);
DevastatedCommented:
grrrrrrr

Response.Write("The kiosk Host Name is " + Request.UserHostAddress);
SHOULD READ
Response.Write("The kiosk Host Name is " + Request.UserHostName);

for completeness
Response.Write("The kiosk Host Address is " + Request.UserHostAddress);
trevorhartmanAuthor Commented:
Yeah i tried that - from my first post:

" I tried Request.UserHostName but that returns the IP address, identical to Request.UserHostAddress. "

thx anyway

-Trevor
DevastatedCommented:
ah hah!.... hehe soz my mistake...

but therein i have more info on your problem...

Request.UserHostName - returns a string that contains the 'DNS' hostname...

DNS i hear u say!?

The problem you are having then is that the DNS server must be available and be able to resolve the client ip to a DNS name - if it cannot resolve it it returns the IP address as if you used Request.UserHostAddress.

so it seems theres a DNS resolution problem on your network.
trevorhartmanAuthor Commented:
could be, but this:

               string kioskHostIP = Request.UserHostAddress;
               
               // do a reverse lookup to figure the HostName using the IP address
               IPHostEntry hostInfo = Dns.GetHostByAddress(kioskHostIP);
               string kioskHostName = hostInfo.HostName;
               Response.Write(kioskHostName + "<br>");

works for me so i'm happy :)
DevastatedCommented:
sweet :) -- its a lesson i am yet to learn -- if it works it works :p
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.