using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RestSharp; namespace IotManager.Common { public class BaiduAPI { /// /// 百度api /// /// public static string GetBaiduIp(string ip) { string location = ""; try { string url = $"https://sp0.baidu.com"; //WebClient client = new WebClient(); RestSharp.RestClient client1 = new RestSharp.RestClient(url); RestSharp.RestRequest request = new RestSharp.RestRequest($"/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&co=&resource_id=6006&oe=utf8", Method.Get); var buffer = client1.DownloadData(request); //var buffer = client.DownloadData(url); string jsonText = Encoding.UTF8.GetString(buffer); JObject jo = JObject.Parse(jsonText); Root root = JsonConvert.DeserializeObject(jo.ToString()); foreach (var item in root.data) { location = item.location; } return location; } catch (Exception ex) { //Console.WriteLine(ex); return location; } } } public class Root { public List data { get; set; } } public class DataItem { /// /// /// public string ExtendedLocation { get; set; } /// /// /// public string OriginQuery { get; set; } /// /// /// public string appinfo { get; set; } /// /// /// public int disp_type { get; set; } /// /// /// public string fetchkey { get; set; } /// /// 本地局域网 /// public string location { get; set; } /// /// /// public string origip { get; set; } /// /// /// public string origipquery { get; set; } /// /// /// public string resourceid { get; set; } /// /// /// public int role_id { get; set; } /// /// /// public int shareImage { get; set; } /// /// /// public int showLikeShare { get; set; } /// /// /// public string showlamp { get; set; } /// /// IP地址查询 /// public string titlecont { get; set; } /// /// /// public string tplt { get; set; } } }