How to get User Url, User Host Name and User Host Address?

Posted by Gopesh9 under ASP.NET category on | Points: 40 | Views : 4332
TextBox1.Text = Request.Url.ToString();
TextBox2.Text = Request.UserHostAddress;
TextBox3.Text = Request.UserHostName;


//Add the Namespace
using System.Net;

//Get Visitor IP address method
public string GetVisitorIpAddress()
string stringIpAddress;
stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
return "Your ip is "+stringIpAddress;


//Get Lan Connected IP address method
public string GetLanIPAddress()
//Get the Host Name
string stringHostName = Dns.GetHostName();
//Get The Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;
return arrIpAddress[arrIpAddress.Length - 1].ToString();


//Get The Visitor Ip Address
string strVisitorIpAddress = GetVisitorIpAddress();

//Get The Lan Ip Address
string strLanIpAddress = GetLanIPAddress();

Comments or Responses

Posted by: Srilu.Nayini577 on: 8/6/2012 Level:Starter | Status: [Member] | Points: 10
Nice post.It's very useful.

Thank you,

Login to post response