Pages

Thursday, July 30, 2009

IP retriever with proxy detection in .Net

Every visitor of your website has an IP address

Here is the code to retrieve it

(C#)

public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;

VB.Net

Public Function IpAddress()
Dim strIpAddress As String
strIpAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If strIpAddress = "" Then
strIpAddress = Request.ServerVariables("REMOTE_ADDR")
End If
IpAddress = strIpAddress
End Function
}

Getting Location using Webservice

Then I found the following web service which provide this service absolutely free and that too without any complex interface to do the same

http://freegeoip.appspot.com/

The above website provides free IP Geolocation Web Service that returns data in three formats .

1. XML [Extended Markup Language]
2. CSV [Comma Separated Values]
3. JSON [JavaScript Object Notation]

Here I am explaining how to get the data in XML format.

Calling the web service is also easy you just need to pass the IP Address in URL and it will display the data

http://freegeoip.appspot.com/xml/122.169.8.137

There are other free services too that return data in same format e.g.

http://ipinfodb.com/ip_query.php?ip=122.169.8.137

Both the above returns you the output in XML Format for the IP Address in the URL

The returned XML looks as below



true
122.169.8.137
IN
India
16
Maharashtra
Bombay

18.975
72.8258


As you can see with the IP Address you can find

1. Country
2. City
3. Region
4. Latitude
5. Longitude

Now I’ll explain how to consume this xml and display data on web page in asp.net.

Below function GetLocation creates a WebRequest and WebProxy and make a call to the url

Then the xml response is received as WebResponse and then Xml in WebResponse is read by the XMLTextReader and finally filled into a DataSet.

C#

private DataTable GetLocation(string ipaddress)
{
//Create a WebRequest
WebRequest rssReq =
WebRequest.Create("http://freegeoip.appspot.com/xml/"
+ ipaddress);

//Create a Proxy
WebProxy px =
new WebProxy("http://freegeoip.appspot.com/xml/"
+ ipaddress, true);

//Assign the proxy to the WebRequest
rssReq.Proxy = px;

//Set the timeout in Seconds for the WebRequest
rssReq.Timeout = 2000;
try
{
//Get the WebResponse
WebResponse rep = rssReq.GetResponse();

//Read the Response in a XMLTextReader
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

//Create a new DataSet
DataSet ds = new DataSet();

//Read the Response into the DataSet
ds.ReadXml(xtr);
return ds.Tables[0];
}
catch
{
return null;
}
}

VB.Net

Private Function GetLocation(ByVal ipaddress As String) _
As DataTable
'Create a WebRequest
Dim rssReq As WebRequest = _
WebRequest.Create("http://freegeoip.appspot.com/xml/" _
& ipaddress)

'Create a Proxy
Dim px As New WebProxy("http://freegeoip.appspot.com/xml/" _
& ipaddress, True)

'Assign the proxy to the WebRequest
rssReq.Proxy = px

'Set the timeout in Seconds for the WebRequest
rssReq.Timeout = 2000
Try
'Get the WebResponse
Dim rep As WebResponse = rssReq.GetResponse()

'Read the Response in a XMLTextReader
Dim xtr As New XmlTextReader(rep.GetResponseStream())

'Create a new DataSet
Dim ds As New DataSet()

'Read the Response into the DataSet
ds.ReadXml(xtr)
Return ds.Tables(0)
Catch
Return Nothing
End Try
End Function

First I am retrieving the visitor's IP Address and then based on the IP Address I am finding the Location using the GetLocation function which returns a datatable. Then finally I check if the DataTable has rows, if it has it the data is displayed in Label

C#

//Get IP Address
string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];

DataTable dt = GetLocation(ipaddress);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
lblCity.Text = dt.Rows[0]["City"].ToString();
lblRegion.Text = dt.Rows[0]["RegionName"].ToString();
lblCountry.Text = dt.Rows[0]["CountryName"].ToString();
lblCountryCode.Text = dt.Rows[0]["CountryCode"].ToString();
}
else
{

}
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'Get Ip address
Dim ipaddress As String
ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If ipaddress = "" Or ipaddress Is Nothing Then
ipaddress = Request.ServerVariables("REMOTE_ADDR")
End If
Dim dt As DataTable = GetLocation(ipaddress)
If dt IsNot Nothing Then
If dt.Rows.Count > 0 Then
lblCity.Text = dt.Rows(0)("City").ToString()
lblRegion.Text = dt.Rows(0)("RegionName").ToString()
lblCountry.Text = dt.Rows(0)("CountryName").ToString()
lblCountryCode.Text = dt.Rows(0)("CountryCode").ToString()
Else
End If
End If
End Sub

0 comments:

Post a Comment