コード例 #1
0
ファイル: IpCityBLL.cs プロジェクト: linchh/MainBlog
        /// <summary>
        /// 根据ip地址取得对应的地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        static public string GetIpCityStr(string ip)
        {
            try{
                string city = string.Empty;

                Models.IpCityModel CityModel = new Models.IpCityModel();
                CityModel = ApiBLL.IpCityBLL.GetIpLocationAPI(ip);

                if (CityModel != null)
                {
                    if (CityModel.Status == 0)
                    {
                        Hashtable hs = Newtonsoft.Json.JsonConvert.DeserializeObject <Hashtable>(CityModel.Result["ad_info"].ToString());
                        foreach (IDictionary a in hs)
                        {
                            if (a.Keys.ToString().ToLower() == "adcode".ToLower())
                            {
                                continue;
                            }
                            city += a.Values.ToString();
                        }
                    }
                }
                return(city);
            }
            catch (Exception ex) {
                ErrorMsg = ex.Message;
                BLL.BaseBLL.ErrorLog("请求IP地址解析时发生错误", "ApiBLL->IpCityBLL->GetIpCityStr", ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: IpCityBLL.cs プロジェクト: linchh/MainBlog
 /// <summary>
 /// 根据ip地址取得API结果模型
 /// </summary>
 /// <param name="ip"></param>
 /// <returns></returns>
 static public Models.IpCityModel GetIpLocationAPI(string ip)
 {
     try{
         Models.IpCityModel model      = new Models.IpCityModel();
         string             requestUrl = MyTools.Tool.GetAppSetting("IpCityUrl");
         string             key        = MyTools.Tool.GetAppSetting("IpCityKey");
         string             url        = $@"{requestUrl}?ip={ip}&key={key}";
         string             resStr     = MyTools.Tool.HttpGet(url);
         model = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.IpCityModel>(resStr);
         return(model);
     }
     catch (Exception ex) {
         ErrorMsg = ex.Message;
         BLL.BaseBLL.ErrorLog("请求IP地址解析时发生错误", "ApiBLL->IpCityBLL->GetIpLocationAPI", ex);
         return(null);
     }
 }