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.io IP Geolocation API to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our API.

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

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestASPDotNetIP2LocationIOAPICSharp
{
	public partial class WebForm1 : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			WebAPI();
		}

		private void WebAPI()
		{
			string strQuery;
			string IPAddress = HttpContext.Current.Request.UserHostAddress;
			string Key = "YOUR_API_KEY";
			WebRequest HttpWReq;
			System.Net.WebResponse HttpWResp;

			strQuery = "https://api.ip2location.io/?" + "ip=" + IPAddress + "&key=" + Key + "&format=json";
			HttpWReq = WebRequest.Create(strQuery);
			HttpWReq.Method = "GET";
			try
			{
				HttpWResp = HttpWReq.GetResponse();
				System.IO.StreamReader reader = new System.IO.StreamReader(HttpWResp.GetResponseStream());
				string content = reader.ReadToEnd();

				JObject results = JsonConvert.DeserializeObject<JObject>(content);
				Response.Write("ip: " + results["ip"].ToString() + "<br>\n");
				Response.Write("country_code: " + results["country_code"].ToString() + "<br>\n");
				Response.Write("country_name: " + results["country_name"].ToString() + "<br>\n");
				Response.Write("region_name: " + results["region_name"].ToString() + "<br>\n");
				Response.Write("city_name: " + results["city_name"].ToString() + "<br>\n");
				Response.Write("latitude: " + results["latitude"].ToString() + "<br>\n");
				Response.Write("longitude: " + results["longitude"].ToString() + "<br>\n");
				Response.Write("zip_code: " + results["zip_code"].ToString() + "<br>\n");
				Response.Write("time_zone: " + results["time_zone"].ToString() + "<br>\n");
				Response.Write("asn: " + results["asn"].ToString() + "<br>\n");
				Response.Write("as: " + results["as"].ToString() + "<br>\n");
				if (results["isp"] != null)
					Response.Write("isp: " + results["isp"].ToString() + "<br>\n");
				if (results["domain"] != null)
					Response.Write("domain: " + results["domain"].ToString() + "<br>\n");
				if (results["net_speed"] != null)
					Response.Write("net_speed: " + results["net_speed"].ToString() + "<br>\n");
				if (results["idd_code"] != null)
					Response.Write("idd_code: " + results["idd_code"].ToString() + "<br>\n");
				if (results["area_code"] != null)
					Response.Write("area_code: " + results["area_code"].ToString() + "<br>\n");
				if (results["weather_station_code"] != null)
					Response.Write("weather_station_code: " + results["weather_station_code"].ToString() + "<br>\n");
				if (results["weather_station_name"] != null)
					Response.Write("weather_station_name: " + results["weather_station_name"].ToString() + "<br>\n");
				if (results["mcc"] != null)
					Response.Write("mcc: " + results["mcc"].ToString() + "<br>\n");
				if (results["mnc"] != null)
					Response.Write("mnc: " + results["mnc"].ToString() + "<br>\n");
				if (results["mobile_brand"] != null)
					Response.Write("mobile_brand: " + results["mobile_brand"].ToString() + "<br>\n");
				if (results["elevation"] != null)
					Response.Write("elevation: " + results["elevation"].ToString() + "<br>\n");
				if (results["usage_type"] != null)
					Response.Write("usage_type: " + results["usage_type"].ToString() + "<br>\n");
				if (results["address_type"] != null)
					Response.Write("address_type: " + results["address_type"].ToString() + "<br>\n");
				if (results["district"] != null)
					Response.Write("district: " + results["district"].ToString() + "<br>\n");
				if (results["ads_category"] != null)
					Response.Write("ads_category: " + results["ads_category"].ToString() + "<br>\n");
				if (results["ads_category_name"] != null)
					Response.Write("ads_category_name: " + results["ads_category_name"].ToString() + "<br>\n");
				Response.Write("is_proxy: " + results["is_proxy"].ToString() + "<br>\n");

				if (results["continent"] != null)
				{
					var Continent = results["continent"];
					Response.Write("continent => name: " + Continent["name"].ToString() + "<br>\n");
					Response.Write("continent => code: " + Continent["code"].ToString() + "<br>\n");
					Response.Write("continent => hemisphere: " + Continent["hemisphere"].ToString() + "<br>\n");
					Response.Write("continent => translation => lang: " + Continent["translation"]["lang"].ToString() + "<br>\n");
					Response.Write("continent => translation => value: " + Continent["translation"]["value"].ToString() + "<br>\n");
				}

				if (results["country"] != null)
				{
					var Country = results["country"];
					Response.Write("country => name: " + Country["name"].ToString() + "<br>\n");
					Response.Write("country => alpha3_code: " + Country["alpha3_code"].ToString() + "<br>\n");
					Response.Write("country => numeric_code: " + Country["numeric_code"].ToString() + "<br>\n");
					Response.Write("country => demonym: " + Country["demonym"].ToString() + "<br>\n");
					Response.Write("country => flag: " + Country["flag"].ToString() + "<br>\n");
					Response.Write("country => capital: " + Country["capital"].ToString() + "<br>\n");
					Response.Write("country => total_area: " + Country["total_area"].ToString() + "<br>\n");
					Response.Write("country => population: " + Country["population"].ToString() + "<br>\n");
					Response.Write("country => tld: " + Country["tld"].ToString() + "<br>\n");
					Response.Write("country => currency => code: " + Country["currency"]["code"].ToString() + "<br>\n");
					Response.Write("country => currency => name: " + Country["currency"]["name"].ToString() + "<br>\n");
					Response.Write("country => currency => symbol: " + Country["currency"]["symbol"].ToString() + "<br>\n");
					Response.Write("country => language => code: " + Country["language"]["code"].ToString() + "<br>\n");
					Response.Write("country => language => name: " + Country["language"]["name"].ToString() + "<br>\n");
					Response.Write("country => translation => lang: " + Country["translation"]["lang"].ToString() + "<br>\n");
					Response.Write("country => translation => value: " + Country["translation"]["value"].ToString() + "<br>\n");
				}

				if (results["region"] != null)
				{
					var Region = results["region"];
					Response.Write("region => name: " + Region["name"].ToString() + "<br>\n");
					Response.Write("region => code: " + Region["code"].ToString() + "<br>\n");
					Response.Write("region => translation => lang: " + Region["translation"]["lang"].ToString() + "<br>\n");
					Response.Write("region => translation => value: " + Region["translation"]["value"].ToString() + "<br>\n");
				}

				if (results["city"] != null)
				{
					var City = results["city"];
					Response.Write("city => name: " + City["name"].ToString() + "<br>\n");
					Response.Write("city => translation => lang: " + City["translation"]["lang"].ToString() + "<br>\n");
					Response.Write("city => translation => value: " + City["translation"]["value"].ToString() + "<br>\n");
				}

				if (results["time_zone_info"] != null)
				{
					var TimeZone = results["time_zone_info"];
					Response.Write("time_zone_info => olson: " + TimeZone["olson"].ToString() + "<br>\n");
					Response.Write("time_zone_info => current_time: " + TimeZone["current_time"].ToString() + "<br>\n");
					Response.Write("time_zone_info => gmt_offset: " + TimeZone["gmt_offset"].ToString() + "<br>\n");
					Response.Write("time_zone_info => is_dst: " + TimeZone["is_dst"].ToString() + "<br>\n");
					Response.Write("time_zone_info => sunrise: " + TimeZone["sunrise"].ToString() + "<br>\n");
					Response.Write("time_zone_info => sunset: " + TimeZone["sunset"].ToString() + "<br>\n");
				}

				if (results["geotargeting"] != null)
				{
					var GeoTarget = results["geotargeting"];
					Response.Write("geotargeting => metro: " + GeoTarget["metro"].ToString() + "<br>\n");
				}

				if (results["proxy"] != null)
				{
					var Proxy = results["proxy"];
					Response.Write("proxy => last_seen: " + Proxy["last_seen"].ToString() + "<br>\n");
					Response.Write("proxy => proxy_type: " + Proxy["proxy_type"].ToString() + "<br>\n");
					Response.Write("proxy => threat: " + Proxy["threat"].ToString() + "<br>\n");
					Response.Write("proxy => provider: " + Proxy["provider"].ToString() + "<br>\n");
					Response.Write("proxy => is_vpn: " + Proxy["is_vpn"].ToString() + "<br>\n");
					Response.Write("proxy => is_tor: " + Proxy["is_tor"].ToString() + "<br>\n");
					Response.Write("proxy => is_data_center: " + Proxy["is_data_center"].ToString() + "<br>\n");
					Response.Write("proxy => is_public_proxy: " + Proxy["is_public_proxy"].ToString() + "<br>\n");
					Response.Write("proxy => is_web_proxy: " + Proxy["is_web_proxy"].ToString() + "<br>\n");
					Response.Write("proxy => is_web_crawler: " + Proxy["is_web_crawler"].ToString() + "<br>\n");
					Response.Write("proxy => is_residential_proxy: " + Proxy["is_residential_proxy"].ToString() + "<br>\n");
					Response.Write("proxy => is_spammer: " + Proxy["is_spammer"].ToString() + "<br>\n");
					Response.Write("proxy => is_scanner: " + Proxy["is_scanner"].ToString() + "<br>\n");
					Response.Write("proxy => is_botnet: " + Proxy["is_botnet"].ToString() + "<br>\n");
					Response.Write("proxy => is_consumer_privacy_network: " + Proxy["is_consumer_privacy_network"].ToString() + "<br>\n");
					Response.Write("proxy => is_enterprise_private_network: " + Proxy["is_enterprise_private_network"].ToString() + "<br>\n");
				}
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}

	}
}

Was this article helpful?

Related Articles