Tuesday 7 November 2017

Get All Location Details Using IPAddress in C#.

This article will tell you about how to retrieve User Locations by passing IPAddress.

http://freegeoip.net is a free and open-source URL and by using which we can get User location like Latitude,Longitude,Country Name etc and we will check all while we will implement.

For more details you can visit this URL: http://freegeoip.net

We can retrieve the data from this URL either in XML format or json format.

XML Format :-
-----------------

First we need to include using System.Net; namespace.

 As we are using XML Format so we need to use the URL like this, http://freegeoip.net/xml/ and after that we pass ipaddress. By using of WebRequest and WebResponse we can retrieve data and as we are using XML format so by Using ReadXml method we will retrieve the data and atlast we binded the data to dataset and from there we can retrieve the data.

JSON Format :- 
---------------------

To get the data in JSON format we can use two technique such as:

i/ WebClient

ii/ HttpClient

        i/ WebClient :-
-------------------

In this above code we used the URL like this, http://freegeoip.net/json/ because we are using here json format of data and when we will call this line var json = webClient.DownloadString(URL); then after that we will retrieve the data from our URL and atlast we need to convert it into json format. To Convert into json format we need to use using Newtonsoft.Json; namespace.

       ii/ HttpClient :-
       ---------------------

In the above code we used the same URL but here we used the HttpClient and by using which we can retrieve the json data.

This URL http://freegeoip.net will give you the following information as a response,

  • IP
  • CountryCode
  • CountryName
  • RegionCode
  • RegionName
  • City
  • ZipCode
  • TimeZone
  • Latitude
  • Longitude
  • MetroCode
and after that you can easily use in your project. 

0 comments:

Post a Comment