Location实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Location 2 { 3 public virtual string Province { get; set; } 4 5 public virtual string City { get; set; } 6 }
腾讯IP返回代码实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class TencentResult 2 { 3 public int status { get; set; } 4 public string message { get; set; } 5 public Result result { get; set; } 6 7 public class Result 8 { 9 public string ip { get; set; } 10 public Location location { get; set; } 11 public AdInfo ad_Info { get; set; } 12 13 public class Location 14 { 15 public string lat { get; set; } 16 public string lng { get; set; } 17 } 18 19 public class AdInfo 20 { 21 public string nation { get; set; } 22 public string province { get; set; } 23 public string city { get; set; } 24 public string district { get; set; } 25 public string adcode { get; set; } 26 } 27 } 28 }
百度IP返回实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class BaiduResult 2 { 3 public string address { get; set; } 4 public Content content { get; set; } 5 public int status { get; set; } 6 7 public class Content 8 { 9 public string address { get; set; } 10 public AddressDetail address_detail { get; set; } 11 public Point point { get; set; } 12 13 public class AddressDetail 14 { 15 public string city { get; set; } 16 public string city_code { get; set; } 17 public string province { get; set; } 18 } 19 public class Point 20 { 21 public string x { get; set; } 22 public string y { get; set; } 23 } 24 } 25 }
高德IP返回实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class GaodeResult 2 { 3 public int status { get; set; } 4 public string info { get; set; } 5 public string infocode { get; set; } 6 public object province { get; set; } 7 public object city { get; set; } 8 public object adcode { get; set; } 9 public object rectangle { get; set; } 10 }
淘宝IP返回实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class TaobaoResult 2 { 3 public Data data { get; set; } 4 public string msg { get; set; } 5 public int code { get; set; } 6 public class Data 7 { 8 public string area { get; set; } 9 public string country { get; set; } 10 public string isp_id { get; set; } 11 public string queryIp { get; set; } 12 public string city { get; set; } 13 public string ip { get; set; } 14 public string isp { get; set; } 15 public string county { get; set; } 16 public string region_id { get; set; } 17 public string area_id { get; set; } 18 public string county_id { get; set; } 19 public string region { get; set; } 20 public string country_id { get; set; } 21 public string city_id { get; set; } 22 } 23 }
通过IP获取省市
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 通过Ip获取省市 3 /// </summary> 4 /// <returns></returns> 5 public virtual async Task<Location> GetLocationByIp(string ip) 6 { 7 if (ip == "::1" || ip == "127.0.0.1" || ip.StartsWith("192.168.")) 8 { 9 return new Location 10 { 11 Province = "350000", 12 City = "350500" 13 }; 14 } 15 var tencentKey = "111"; 16 var tencentSK = "222"; 17 var baiduAK = "333"; 18 string gaodeKey = "444"; 19 Location location = null; 20 using var httpClient = _httpClientFactory.CreateClient(); 21 if (location == null)//腾讯:腾讯获取ip用的是sign校验方式,每日300W,500QPS 22 { 23 var sig = MD5Encrypt(string.Format("/ws/location/v1/ip?ip={0}&key={1}{2}", ip, tencentKey, tencentSK)); 24 var url = string.Format("https://apis.map.qq.com/ws/location/v1/ip?ip={0}&key={1}&sig={2}", ip, tencentKey, sig); 25 var tencentResult = JsonConvert.DeserializeObject<TencentResult>(await httpClient.GetStringAsync(url)); 26 if (tencentResult == null) 27 location = null; 28 location = new Location 29 { 30 Province = tencentResult.result.ad_Info.province, 31 City = tencentResult.result.ad_Info.city 32 }; 33 } 34 if (location == null || (string.IsNullOrEmpty(location.City) || string.IsNullOrEmpty(location.Province)))//百度:百度获取ip用的是白名单校验发起调用方式,每日30W,300QPS 35 { 36 var sn_url = string.Format("http://api.map.baidu.com/location/ip?ak={0}&ip={1}&coor=bd09ll", baiduAK, ip); 37 var baiduResult = JsonConvert.DeserializeObject<BaiduResult>(await httpClient.GetStringAsync(sn_url)); 38 if (baiduResult.content == null) { 39 location = null; 40 } 41 else 42 { 43 location = new Location 44 { 45 Province = baiduResult.content.address_detail.province, 46 City = baiduResult.content.address_detail.city 47 }; 48 } 49 } 50 if (location == null || (string.IsNullOrEmpty(location.City) || string.IsNullOrEmpty(location.Province)))//高德:每日有免费300万次IP定位服务 51 { 52 string gaode_url = string.Format("https://restapi.amap.com/v3/ip?ip={0}&key={1}&output=JSON", ip, gaodeKey); 53 var result = await httpClient.GetStringAsync(gaode_url); 54 GaodeResult gaodeResult = JsonConvert.DeserializeObject<GaodeResult>(result); 55 if (gaodeResult == null) { 56 location = null; 57 } 58 else 59 { 60 location = new Location 61 { 62 Province = gaodeResult.province + "" == "[]" ? "" : gaodeResult.province + "", 63 City = gaodeResult.city + "" == "[]" ? "" : gaodeResult.city + "" 64 }; 65 } 66 } 67 if (location == null || (string.IsNullOrEmpty(location.City) || string.IsNullOrEmpty(location.Province)))//淘宝:没有次数限制,但是并发1QPS只能一次,即一秒只能查询一次 68 { 69 string taobao_url = "http://ip.taobao.com/outGetIpInfo"; 70 List<KeyValuePair<string, string>> kvpsList = new List<KeyValuePair<string, string>>(); 71 kvpsList.Add(new KeyValuePair<string, string>("ip", ip)); 72 kvpsList.Add(new KeyValuePair<string, string>("accessKey", "alibaba-inc")); 73 var formContent = new FormUrlEncodedContent(kvpsList); 74 var taobaoResponseJson = httpClient.PostAsync(taobao_url, formContent).Result.Content.ReadAsStringAsync().Result; 75 TaobaoResult taobaoResult = JsonConvert.DeserializeObject<TaobaoResult>(taobaoResponseJson); 76 if (taobaoResult == null) { 77 throw new UserFriendlyException("未查询到ip"); 78 } 79 else 80 { 81 location = new Location 82 { 83 Province = taobaoResult.data.region, 84 City = taobaoResult.data.city 85 }; 86 } 87 } 88 return new Location 89 { 90 Province = CityData.GetProvinceCode(location.Province), 91 City = CityData.GetCityCode(location.City) 92 }; 93 } 94 95 protected virtual string MD5Encrypt(string password) 96 { 97 var md5Hasher = new MD5CryptoServiceProvider(); 98 99 var hashedDataBytes = md5Hasher.ComputeHash( 100 Encoding.Default.GetBytes(password)); 101 102 var result = new StringBuilder(); 103 foreach (byte i in hashedDataBytes) 104 { 105 result.Append(i.ToString("x2")); 106 } 107 108 return result.ToString(); 109 }