Sunday, September 23, 2018

Geolocation in C# and ASP.NET by IP address

Well here I am going to share simple method of geolocation by web page visitors IP address.
The method uses http://ip-api.com/  service which is free for use geolocation database and API (have a look at it it's worthed project).


   public static string GeoLocate()
        {
            string linfo = "N/A";

            try
            {
                WebRequest request = WebRequest.Create("http://ip-api.com/xml/" + Request.UserHostAddress);
                request.Credentials = CredentialCache.DefaultCredentials;
                WebResponse response = request.GetResponse();

                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);

                linfo = reader.ReadToEnd();

                reader.Close();
                response.Close();

                int countryFrom = linfo.IndexOf("<country>");
                int countryTo = linfo.IndexOf("</country>");

                string Country = String.Empty;

                if (countryFrom != -1)
                {
                    Country = linfo.Substring(countryFrom, countryTo - countryFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<country>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                int cityFrom = linfo.IndexOf("<city>");
                int cityTo = linfo.IndexOf("</city>");

                string City = String.Empty;

                if (cityFrom != -1)
                {
                    City = linfo.Substring(cityFrom, cityTo - cityFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<city>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                int regionFrom = linfo.IndexOf("<regionName>");
                int regionTo = linfo.IndexOf("</regionName>");

                string Region = String.Empty;

                if (regionFrom != -1)
                {
                    Region = linfo.Substring(regionFrom, regionTo - regionFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<regionName>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                int latFrom = linfo.IndexOf("<lat>");
                int latTo = linfo.IndexOf("</lat>");

                string Latitude = String.Empty;

                if (latFrom != -1)
                {
                    Latitude = linfo.Substring(latFrom, latTo - latFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<lat>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                int longFrom = linfo.IndexOf("<lon>");
                int longTo = linfo.IndexOf("</lon>");

                string Longitude = String.Empty;

                if (longFrom != -1)
                {
                    Longitude = linfo.Substring(longFrom, longTo - longFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<lon>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                int orgFrom = linfo.IndexOf("<org>");
                int orgTo = linfo.IndexOf("</org>");

                string Organization = String.Empty;

                if (orgFrom != -1)
                {
                    Organization = linfo.Substring(orgFrom, orgTo - orgFrom).TrimEnd(Environment.NewLine.ToCharArray()).Replace("<org>", String.Empty).Replace("<![CDATA[", String.Empty).Replace("]]>", String.Empty);
                }

                linfo = "Country: " + Country  + " Region: " + Region + " City: " + City + " Organization: "  + " Latitude: " + Latitude +  " Longitude: " + Longitude;
            }
            catch { }

            return linfo;
        }

I hope it helps, anyway please let me know.

No comments:

Post a Comment