In
this article we will see how to find the IP address of the client using asp.net.
For getting the IP Address of the client write the following code.
using System;
using System.Net; //Add Namespace
public partial class IpAddress : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get The
Visitor Ip Address
String IpAddress =
Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
IF (IpAddress == null
|| IpAddress ==
String.Empty) //if HTTP_X_FORWARDED_FOR is null
{
//we can use REMOTE_ADDR
IpAddress =
Request.ServerVariables["REMOTE_ADDR"];
}
//Get
the Host Name
String HostName = Dns.GetHostName();
//Get
The Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(HostName);
//Get
The Ip Address
IPAddress[] arrayOfIpAddress = ipHostEntries.AddressList;
//Get
The Lan Ip Address
String strLanIpAddress =
arrayOfIpAddress[arrayOfIpAddress.Length - 1].ToString();
Response.Write("Visitor Ip Address : " + IpAddress
+ "<br/> Your Host Name : " + HostName
+ "<br/> Your Ip Address : " + strLanIpAddress);
}
}
For
getting the IP Address of the client we have to make a request to http://www.whatismyip.org. Write the
following code in Page_Load event.
System.Net.HttpWebRequest
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.whatismyip.org");
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
String strExternalIP = string.Empty;
using (System.IO.StreamReader
reader = new StreamReader(response.GetResponseStream()))
{
strExternalIP = reader.ReadToEnd();
reader.Close();
}
Response.Write(strExternalIP.ToString());
No comments:
Post a Comment