Code For Default.aspx.cs Page
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
// string ipaddress;
DataTable dt;
// byte[] byteArray;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Findlocation();
}
}
private void Findlocation()
{
WebClient wc = new WebClient();
string baseurl = "http://checkip.dyndns.org/";
Stream data = wc.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string ipaddress = reader.ReadToEnd();
data.Close();
reader.Close();
ipaddress = ipaddress.Replace("Current IP Address:", "").Replace("<html><head><title>Current IP Check</title></head><body> ", "").Replace("\r\n", "").Replace("</body></html>", "").ToString();
dt = new 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();
}
}
}
private DataTable GetLocation(string ipaddress)
{
dt = new DataTable();
WebRequest wreq = WebRequest.Create("http://api.ipinfodb.com/v2/ip_query.php?key=4066276f57ed6431a8a1cf207d882d06187aa68edf7f2734e1942d4e45450fd5&ip=" + ipaddress + "&timezone=false");
WebProxy proxy = new WebProxy("http://api.ipinfodb.com/v2/ip_query.php?key=4066276f57ed6431a8a1cf207d882d06187aa68edf7f2734e1942d4e45450fd5&ip=" + ipaddress + "&timezone=false", true);
wreq.Proxy = proxy;
wreq.Timeout = 5000;
WebResponse wres = wreq.GetResponse();
XmlTextReader xmlRead = new XmlTextReader(wres.GetResponseStream());
DataSet ds = new DataSet();
ds.ReadXml(xmlRead);
return ds.Tables[0];
}
}
Code For Default.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
City : <asp:Label ID="lblCity" runat="server" ></asp:Label><br />
Region : <asp:Label ID="lblRegion" runat="server" ></asp:Label><br />
Country : <asp:Label ID="lblCountry" runat="server" ></asp:Label><br />
CountryCode : <asp:Label ID="lblCountryCode" runat="server" ></asp:Label>
</div>
</form>
</body>
</html>
0 comments:
Post a Comment