c#獲取訪問者的真實IP地址以及所在地區(二)-------所在地區

在上一篇中我們獲取到了Ip地址,下一步我們就可以根據Ip地址獲取所在地區.

在這裏,我使用第三方定位服務:淘寶的Ip地址庫

只需要調用淘寶的Ip地址庫就能查詢到所在地區.

 

淘寶Ip地址庫鏈接:

http://ip.aliyun.com/instructions.html


 

在上圖中可以看見,我們需要調用的API地址和所需參數以及返回的數據格式

請求接口地址:'' http://ip.taobao.com/service/getIpInfo.php?ip=xxxx',我們只需要提供IP地址即可

在返回的數據格式中,我們需要將其Json數據轉爲實體類.

後臺代碼:

第一步: 通過c#遠程請求數據

方法封裝:WebRequestHeler工具類
public  static class WebRequestHelper

    {
        /// <summary>
        ///以Get方式根據提供的地址  獲取數據
        /// </summary>
        /// <param name="url">以Get方式根據提供的地址</param>
        /// <returns></returns>
        public static string GetUrl(string url)
        {
            string resultstring = string.Empty;
            Encoding encoding = Encoding.UTF8;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//這裏的url指要獲取的數據網址
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    resultstring = reader.ReadToEnd();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return resultstring;
        }
    }

.

第二步:建立實體類.
public class IpAddressInfo

    {
        public int code { get; set; }
        public DataList data { get; set; }
    }
 public class DataList
    {
        public string ip { get; set; }
        public string country { get; set; }
        public string area { get; set; }
        public string region { get; set; }
        public string city { get; set; }
        public string county { get; set; }
        public string isp { get; set; }
        public string country_id { get; set; }
        public string area_id { get; set; }
        public string region_id { get; set; }
        public string city_id { get; set; }
        public string county_id { get; set; }
        public string isp_id { get; set; }
    }
第三步:封裝Json反序列化工具類方法
public static class JsonHelper

                {
                    /// <summary>
                    /// 反序列化 將Json數據轉爲實體類
                    /// </summary>
                    /// <typeparam name="T"></typeparam>
                    /// <param name="jsonStr"></param>
                    /// <returns></returns>
                    public static T StringToEnteity<T>(string jsonStr)  where T : class
                    {
                        try
                        {
                            return JsonConvert.DeserializeObject<T>(jsonStr);
                        }
                        catch (Exception ex)
                        {
                            string error = ex.Message;
                            return null;
                        }
                    }
              }

 

第四步:根據Ip獲取真實的地址.

//假定IP地址是:120.79.252.67

string IpAddress = "120.79.252.67";//模擬獲取訪問者的Ip地址
//拼接查詢路徑
string url="http://ip.taobao.com/service/getIpInfo.php?ip="+IpAddress +"";
//調用遠程訪問Get方法
string strJson=WebRequestHelper.GetUrl(url);//返回Json數據
//反序列化  取得返回結果
IpAddressInfo inf = JsonHelper.StringToEnteity<IpAddressInfo>(strJson);
//然後即可獲取所需要的信息
//比如:
//國家:inf.data.country;
//省:inf.data.region;
//市:inf.data.city;
//通訊網絡;inf.data.isp

原文地址:

https://www.zddblog.top/Home/Detail?art_id=Mjk=

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章