1. 程式人生 > >c#獲取訪問者的真實IP地址以及所在地區(二)-------所在地區

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=