Saturday, March 10, 2012

City State Country From public IP Address ASP.NET

To get City, State, Country, details From public IP Address first we need to get PublicIp as mentioned this article

clients public ip address in asp.net (C#)

Next to get the details free registration for your website is required to get API key.

If you don’t have an API key yet, just go to here and register for your free API key. You will need this API key to be able to use our newer APIs.


    private string[] res1(string ipAddress)
    {
        try
        {
            string path = "http://api.ipinfodb.com/v3/ip-city/?key=YourKey&ip=" + ipAddress;
            WebClient client = new WebClient();
            string[] eResult = client.DownloadString(path).ToString().Split(';');
            return eResult;
        }
        catch (Exception ex)
        {
            string errmsg = ex.Message;
            return null;
        }
    }
}

In the pageLoad event write this code
string ipAddress = Click Here
 string[] res = res1(ipAddress);
        if (res.Length > 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table><tr><td><b>");
            sb.Append("My Public IP </td><td> : " + res[2] + "</td></tr>");
            sb.Append("<tr><td><b>");
            sb.Append("Country </td><td> : " + res[4] + "</td></tr>");
            sb.Append("<tr><td><b>");
            sb.Append("State </td><td> : " + res[5] + "</td></tr>");
            sb.Append("<tr><td><b>");
            sb.Append("City </td><td> : " + res[6] + "</td></tr>");
            sb.Append("<tr><td><b>");
            sb.Append("Postal Code </td><td> : " + res[7] + "</td></tr>");
            sb.Append("<tr><td><b>");
            sb.Append("Time Zone </td><td> : " + res[10] + "</td></tr>");
            sb.Append("</table>");
            Label1.Text = sb.ToString();
        }

No comments:

Post a Comment