Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$ curl "https://tools.keycdn.com/geo.json?host={IP or hostname}"


This is the cURL.
This is used to get the location of the user by IP address.
However, i do not know how to convert this to asp.net C#.

What I have tried:

I have tried the following codes:

public IPData GetIPGeoLocation(string IP)
       {
           WebClient client = new WebClient();
           // Make an api call and get response.
           try
           {
               string response client.DownloadString("https://tools.keycdn.com/geo.json?host={"+IP+"}");
               //Deserialize response JSON
               IPData ipdata = JsonConvert.DeserializeObject<IPData>(response);
               if (ipdata.status == "fail")
               {
                   throw new Exception("Invalid IP");
               }

               return ipdata;
           }
           catch (Exception)
           {
               throw;
           }
       }


       public class IPData
       {
           public string status { get; set; }
           public string country { get; set; }
           public string countryCode { get; set; }
           public string region { get; set; }
           public string regionName { get; set; }
           public string city { get; set; }
           public string zip { get; set; }
           public string lat { get; set; }
           public string lon { get; set; }
           public string timezone { get; set; }
           public string isp { get; set; }
           public string org { get; set; }
           public string @as { get; set; }
           public string query { get; set; }
       }


Is there something wrong in this part of the code?
Posted
Updated 25-Jul-17 15:43pm

1 solution

I haven't run your code but i think the first place to start would be your class that you are trying to deserialize the response to.

I took the URL and got the json from the site itself and your structure doesn't look correct. I used json2csharp.com and got the following as what you should be using when deserializing.

Json

JavaScript
{
   "status":"success",
   "description":"Data successfully received.",
   "data":{
      "geo":{
         "host":"1.1.1.1",
         "ip":"1.1.1.1",
         "rdns":"hostname",
         "asn":"1111111",
         "isp":"ISP",
         "country_name":"United States",
         "country_code":"US",
         "region":"GA",
         "city":"Atlanta",
         "postal_code":"11111",
         "continent_code":"NA",
         "latitude":"11.1",
         "longitude":"-11.1",
         "dma_code":"111",
         "area_code":"111",
         "timezone":"America\/New_York",
         "datetime":"2017-07-25 21:37:30"
      }
   }
}


Which a matching class to this object would be

C#
public class Geo
{
    public string host { get; set; }
    public string ip { get; set; }
    public string rdns { get; set; }
    public string asn { get; set; }
    public string isp { get; set; }
    public string country_name { get; set; }
    public string country_code { get; set; }
    public string region { get; set; }
    public string city { get; set; }
    public string postal_code { get; set; }
    public string continent_code { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public string dma_code { get; set; }
    public string area_code { get; set; }
    public string timezone { get; set; }
    public string datetime { get; set; }
}

public class Data
{
    public Geo geo { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public string description { get; set; }
    public Data data { get; set; }
}


The class you are using would expect the json returned to simply be

JavaScript
{
   "status":"success",
   "description":"Data successfully received.",
	 "host":"1.1.1.1",
	 "ip":"1.1.1.1",
	 "rdns":"hostname",
	 "asn":"1111111",
	 "isp":"ISP",
	 "country_name":"United States",
	 "country_code":"US",
	 "region":"GA",
	 "city":"Atlanta",
	 "postal_code":"11111",
	 "continent_code":"NA",
	 "latitude":"11.1",
	 "longitude":"-11.1",
	 "dma_code":"111",
	 "area_code":"111",
	 "timezone":"America\/New_York",
	 "datetime":"2017-07-25 21:37:30"
}


and even then, some of the labels you are using in your class don't match the labels that are returned in the json.

But when you deserialize your string to an object instead of IPData ipdata = JsonConvert.DeserializeObject<IPData>(response); you would do

RootObject ipdata = JsonConvert.DeserializeObject<RootObject>(response);

So i think if you swap out your IPData with the class structure above, it may work, again I haven't ran your code myself.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900