我想将以下curl转换为ASP.NET C#
$ curl "https://tools.keycdn.com/geo.json?host={IP or hostname}"
这是cURL。
这用于通过IP地址获取用户的位置。
但是,我不知道如何将其转换为asp.net C#。
我尝试了什么:
我试过以下代码:
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?
curlhttps://tools.keycdn.com/geo.json?host={IP或hostname}
curl "https://tools.keycdn.com/geo.json?host={IP or hostname}"
这是cURL。
这用于通过IP地址获取用户的位置。
但是,我不知道如何将其转换为asp.net C#。
我尝试了什么:
我试过以下代码:
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?
我没有运行你的代码,但我认为第一个开始的地方是你正在尝试反序列化响应的类。
我拿了URL并从网站本身获得了json,你的结构看起来不正确。我使用了json2csharp.com,并在反序列化时应该使用以下内容。
Json
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
{
"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
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; }
}
您正在使用的课程期望返回的json只是
The class you are using would expect the json returned to simply be
{
"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"
}
即便如此,您在班级中使用的某些标签与json中返回的标签不匹配。
但是当你将字符串反序列化为一个对象而不是 IPData ipdata = JsonConvert.DeserializeObject< IPData>(response);
你会这样做 RootObject ipdata = JsonConvert.DeserializeObject< RootObject>(响应);
所以我认为如果你用上面的类结构换掉你的IPData,它可能会起作用,我自己也没有运行你的代码。
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 doRootObject 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.