C# IP v4转地址·地名 高德

需求:

IPv4地址转地址

如:输入14.197.150.014,输出河北省·石家庄市

SDK:

目前使用SDK为高德地图WebAPI

高德地图开放平台icon-default.png?t=N7T8https://lbs.amap.com/

可个人开发者使用,不过有配额限制。

WebAPI 免费配额调整公告icon-default.png?t=N7T8https://lbs.amap.com/news/webapimfpy

流量限制说明icon-default.png?t=N7T8https://lbs.amap.com/api/webservice/guide/tools/flowlevel

API介绍icon-default.png?t=N7T8https://lbs.amap.com/api/webservice/guide/api/ipconfig

请自行在高德后设置白名单,及创建应用。

C# IP v4转地址·地名 高德_第1张图片

请求代码

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";

        /// 
        /// 高德地图IP转地名
        /// https://lbs.amap.com/api/webservice/guide/api/ipconfig
        /// 
        /// 
        public async static Task 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(json);
                }
                else
                {
                    result.status = "0";
                }
            }

            return result;
        }
    }

    [Serializable]
    public class IPLocation
    {
        /// 
        /// 返回结果状态值
        /// 值为0或1,0表示失败;1表示成功
        /// 
        public string status;

        /// 
        /// 返回状态说明
        /// 返回状态说明,status为0时,info返回错误原因,否则返回“OK”。
        /// 
        public string info;

        /// 
        /// 状态码
        /// https://lbs.amap.com/api/ios-sdk/guide/map-tool/errorcode
        /// 
        public string infoCode;

        /// 
        /// 省份名称
        /// 若为直辖市则显示直辖市名称
        /// 如果在局域网 IP网段内,则返回“局域网”
        /// 非法IP以及国外IP则返回空
        /// 
        public string province;

        /// 
        /// 城市名称
        /// 示例:石家庄市
        /// 
        public string city;

        /// 
        /// 城市的adcode编码
        /// 示例:130100
        /// 
        public string adcode;

        /// 
        /// 所在城市矩形区域范围
        /// 所在城市范围的左下右上对标对
        /// 示例:114.2195964,37.86302147;114.7912717,38.22308596
        /// 
        public string rectangle;
    }
}

你可能感兴趣的:(c#,tcp/ip,IP地址,高德)