需求:
IPv4地址转地址
如:输入14.197.150.014,输出河北省·石家庄市
SDK:
目前使用SDK为高德地图WebAPI
高德地图开放平台https://lbs.amap.com/
可个人开发者使用,不过有配额限制。
WebAPI 免费配额调整公告https://lbs.amap.com/news/webapimfpy
流量限制说明https://lbs.amap.com/api/webservice/guide/tools/flowlevel
API介绍https://lbs.amap.com/api/webservice/guide/api/ipconfig
请自行在高德后设置白名单,及创建应用。
请求代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;namespace ShangShangQian.Utility
{public class GaoDeMapAPI{public const string Key = "改成你的KEY";/// <summary>/// 高德地图IP转地名/// https://lbs.amap.com/api/webservice/guide/api/ipconfig/// </summary>/// <param name="ip"></param>public async static Task<IPLocation> GetIPLocation(string ip){IPLocation result = new IPLocation();using (var client = new HttpClient()){string url = $"https://restapi.amap.com/v3/ip?ip={ip}&output=json&key={Key}";var response = await client.GetAsync(url);if (response.IsSuccessStatusCode){string json = await response.Content.ReadAsStringAsync();result = JsonConvert.DeserializeObject<IPLocation>(json);}else{result.status = "0";}}return result;}}[Serializable]public class IPLocation{/// <summary>/// <para>返回结果状态值</para>/// <para>值为0或1,0表示失败;1表示成功</para>/// </summary>public string status;/// <summary>/// <para>返回状态说明</para>/// <para>返回状态说明,status为0时,info返回错误原因,否则返回“OK”。</para>/// </summary>public string info;/// <summary>/// <para>状态码</para>/// <para>https://lbs.amap.com/api/ios-sdk/guide/map-tool/errorcode</para>/// </summary>public string infoCode;/// <summary>/// <para>省份名称</para>/// <para>若为直辖市则显示直辖市名称</para>/// <para>如果在局域网 IP网段内,则返回“局域网”</para>/// <para>非法IP以及国外IP则返回空</para>/// </summary>public string province;/// <summary>/// <para>城市名称</para>/// <para>示例:石家庄市</para>/// </summary>public string city;/// <summary>/// <para>城市的adcode编码</para>/// <para>示例:130100</para>/// </summary>public string adcode;/// <summary>/// <para>所在城市矩形区域范围</para>/// <para>所在城市范围的左下右上对标对</para>/// <para>示例:114.2195964,37.86302147;114.7912717,38.22308596</para>/// </summary>public string rectangle;}
}