Display Visitor’s GeoLocation Using Web Service in C#

Geolocation is a great way to put relevant content to your web visitors and it is a technology that can be used in almost any application you can think of.

In this tutorial, we use the IP2Location™ Web Service to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our hosted web service.

Below are the sample codes written in C# to query and display user’s geolocation information.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebAPI();
    }
 
    private void WebAPI()
    {
        string myIP = HttpContext.Current.Request.UserHostAddress;
        string strQuery;
        string key = "YOUR_API_KEY";
        HttpWebRequest HttpWReq;
        HttpWebResponse HttpWResp;
        strQuery = "https://api.ip2location.com/v2/?" + "ip=" + myIP + "&key=" + key + "&package=WS25&format=json";
        JavaScriptSerializer serializer = new JavaScriptSerializer();
 
        HttpWReq = (HttpWebRequest)WebRequest.Create(strQuery);
        HttpWReq.Method = "GET";
        HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
        System.IO.StreamReader reader = new System.IO.StreamReader(HttpWResp.GetResponseStream());
        string content = reader.ReadToEnd();
        dynamic item = serializer.Deserialize<object>(content);
        string country_code = item["country_code"]
        string country_name = item["country_name"]
        string region_name = item["region_name"]
        string city_name = item["city_name"]
        string latitude = item["latitude"]
        string longitude = item["longitude"]
        string zip_code = item["zip_code"]
        string time_zone = item["time_zone"]
        string isp = item["isp"]
        string domain = item["domain"]
        string net_speed = item["net_speed"]
        string idd_code = item["idd_code"]
        string area_code = item["area_code"]
        string weather_station_code = item["weather_station_code"]
        string weather_station_name = item["weather_station_name"]
        string mcc = item["mcc"]
        string mnc = item["mnc"]
        string mobile_brand = item["mobile_brand"]
        string elevation = item["elevation"]
        string usage_type = item["usage_type"]
        string address_type = item["address_type"]
        string category = item["category"]
        string category_name = item["category_name"]
 
        //displaying the result
        Response.Write("country_code: " + country_code + "<br />");
        Response.Write("country_name: " + country_name + "<br />");
        Response.Write("region_name: " + region_name + "<br />");
        Response.Write("city_name: " + city_name + "<br />");
        Response.Write("latitude: " + latitude + "<br />");
        Response.Write("longitude: " + longitude + "<br />");
        Response.Write("zip_code: " + zip_code + "<br />");
        Response.Write("time_zone: " + time_zone + "<br />");
        Response.Write("isp: " + isp + "<br />");
        Response.Write("domain: " + domain + "<br />");
        Response.Write("net_speed: " + net_speed + "<br />");
        Response.Write("idd_code: " + idd_code + "<br />");
        Response.Write("area_code: " + area_code + "<br />");
        Response.Write("weather_station_code: " + weather_station_code + "<br />");
        Response.Write("weather_station_name: " + weather_station_name + "<br />");
        Response.Write("mcc: " + mcc + "<br />");
        Response.Write("mnc: " + mnc + "<br />");
        Response.Write("mobile_brand: " + mobile_brand + "<br />");
        Response.Write("elevation: " + elevation + "<br />");
        Response.Write("usage_type: " + usage_type + "<br />");
        Response.Write("address_type: " + address_type + "<br />");
        Response.Write("category: " + category + "<br />");
        Response.Write("category_name: " + category_name + "<br />");
    }
 
}

Was this article helpful?

Related Articles