Hi JOYSON,
I have created sample that full-fill your requirement. Refer the below sample code.
CS.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var ip = $('#hfIpAddress').val();
$.ajax({
type: "POST",
url: "CS.aspx/GetLocationDetails",
data: "{ipAddress:'" + ip + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
var table = $("#tblVisitorLocationDetails");
var customers = response.d;
$(customers).each(function () {
$("#ipAddress", table).html(this.IPAddress);
$("#countryName", table).html(this.CountryName);
$("#countryCode", table).html(this.CountryCode);
$("#cityName", table).html(this.CityName);
$("#regionName", table).html(this.RegionName);
$("#zipCode", table).html(this.ZipCode);
$("#latitude", table).html(this.Latitude);
$("#longitude", table).html(this.Longitude);
$("#timeZone", table).html(this.TimeZone);
});
},
failure: function (response) { alert(response.d); },
error: function (response) { alert(response.d); }
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hfIpAddress" runat="server" />
<table id="tblVisitorLocationDetails">
<tr>
<th>
IPAddress
</th>
<th>
CountryName
</th>
<th>
CountryCode
</th>
<th>
CityName
</th>
<th>
RegionName
</th>
<th>
ZipCode
</th>
<th>
Latitude
</th>
<th>
Longitude
</th>
<th>
TimeZone
</th>
</tr>
<tr>
<td>
<span id="ipAddress"></span>
</td>
<td>
<span id="countryName"></span>
</td>
<td>
<span id="countryCode"></span>
</td>
<td>
<span id="cityName"></span>
</td>
<td>
<span id="regionName"></span>
</td>
<td>
<span id="zipCode"></span>
</td>
<td>
<span id="latitude"></span>
</td>
<td>
<span id="longitude"></span>
</td>
<td>
<span id="timeZone"></span>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
CS.aspx.cs
static string APIKey = ConfigurationManager.AppSettings["APIKey"];
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
hfIpAddress.Value = ipAddress;
}
[System.Web.Services.WebMethod]
public static List<Location> GetLocationDetails(string ipAddress)
{
string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
List<Location> locations = new List<Location>();
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
locations.Add(location);
}
return locations;
}
public class Location
{
public string IPAddress { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public string CityName { get; set; }
public string RegionName { get; set; }
public string ZipCode { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string TimeZone { get; set; }
}
VB.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var ip = $('#hfIpAddress').val();
$.ajax({
type: "POST",
url: "VB.aspx/GetLocationDetails",
data: "{ipAddress:'" + ip + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
var table = $("#tblVisitorLocationDetails");
var customers = response.d;
$(customers).each(function () {
$("#ipAddress", table).html(this.IPAddress);
$("#countryName", table).html(this.CountryName);
$("#countryCode", table).html(this.CountryCode);
$("#cityName", table).html(this.CityName);
$("#regionName", table).html(this.RegionName);
$("#zipCode", table).html(this.ZipCode);
$("#latitude", table).html(this.Latitude);
$("#longitude", table).html(this.Longitude);
$("#timeZone", table).html(this.TimeZone);
});
},
failure: function (response) { alert(response.d); },
error: function (response) { alert(response.d); }
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hfIpAddress" runat="server" />
<table id="tblVisitorLocationDetails">
<tr>
<th>
IPAddress
</th>
<th>
CountryName
</th>
<th>
CountryCode
</th>
<th>
CityName
</th>
<th>
RegionName
</th>
<th>
ZipCode
</th>
<th>
Latitude
</th>
<th>
Longitude
</th>
<th>
TimeZone
</th>
</tr>
<tr>
<td>
<span id="ipAddress"></span>
</td>
<td>
<span id="countryName"></span>
</td>
<td>
<span id="countryCode"></span>
</td>
<td>
<span id="cityName"></span>
</td>
<td>
<span id="regionName"></span>
</td>
<td>
<span id="zipCode"></span>
</td>
<td>
<span id="latitude"></span>
</td>
<td>
<span id="longitude"></span>
</td>
<td>
<span id="timeZone"></span>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
VB.aspx.vb
Shared APIKey As String = ConfigurationManager.AppSettings("APIKey")
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If String.IsNullOrEmpty(ipAddress) Then
ipAddress = Request.ServerVariables("REMOTE_ADDR")
End If
hfIpAddress.Value = ipAddress
End Sub
<System.Web.Services.WebMethod()> _
Public Shared Function GetLocationDetails(ipAddress As String) As List(Of Location)
Dim url As String = String.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress)
Dim locations As New List(Of Location)()
Using client As New WebClient()
Dim json As String = client.DownloadString(url)
Dim location As Location = New JavaScriptSerializer().Deserialize(Of Location)(json)
locations.Add(location)
End Using
Return locations
End Function
Public Class Location
Public Property IPAddress() As String
Get
Return m_IPAddress
End Get
Set(value As String)
m_IPAddress = value
End Set
End Property
Private m_IPAddress As String
Public Property CountryName() As String
Get
Return m_CountryName
End Get
Set(value As String)
m_CountryName = value
End Set
End Property
Private m_CountryName As String
Public Property CountryCode() As String
Get
Return m_CountryCode
End Get
Set(value As String)
m_CountryCode = value
End Set
End Property
Private m_CountryCode As String
Public Property CityName() As String
Get
Return m_CityName
End Get
Set(value As String)
m_CityName = value
End Set
End Property
Private m_CityName As String
Public Property RegionName() As String
Get
Return m_RegionName
End Get
Set(value As String)
m_RegionName = value
End Set
End Property
Private m_RegionName As String
Public Property ZipCode() As String
Get
Return m_ZipCode
End Get
Set(value As String)
m_ZipCode = value
End Set
End Property
Private m_ZipCode As String
Public Property Latitude() As String
Get
Return m_Latitude
End Get
Set(value As String)
m_Latitude = value
End Set
End Property
Private m_Latitude As String
Public Property Longitude() As String
Get
Return m_Longitude
End Get
Set(value As String)
m_Longitude = value
End Set
End Property
Private m_Longitude As String
Public Property TimeZone() As String
Get
Return m_TimeZone
End Get
Set(value As String)
m_TimeZone = value
End Set
End Property
Private m_TimeZone As String
End Class
Screenshot
IPAddress | CountryName | CountryCode | CityName | RegionName | ZipCode | Latitude | Longitude | TimeZone |
168.212.226.204 |
United States |
US |
Everett |
Washington |
98204 |
47.8981 |
-122.254 |
-07:00 |