码迷,mamicode.com
首页 > Windows程序 > 详细

C#关于HttpClient的应用(一):获取IP所在的地理位置信息

时间:2016-02-21 15:30:56      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

    public class IpHttpClient:BaseHttpClient
    {
        private String appKey;
        private const string HOST_PATH = "http://apis.baidu.com/apistore/iplookupservice/iplookup";

        public IpHttpClient()
        {
            this.appKey = BaseHelper.GetValue("BaiduAppKey");
        }

        /// <summary>
        /// HTTP 验证
        /// </summary>
        /// <returns></returns>
        public override Dictionary<string, string> Authorization()
        {
            return new Dictionary<string, string> {{"apikey", this.appKey}};
        }

        /// <summary>
        /// 返回当前Ip所在的地理位置信息
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public ResultDTO GetIpAddress(string ip)
        {
            var reqParams = new Dictionary<String, String>
            {
                {"ip", ip}
            };

            var data = this.SendRequest(Method.Get, HOST_PATH, this.BuildQueryStr(reqParams));
            var result= JsonHelper.ToObject(data.info.ToString());
            if (data.status && Convert.ToInt32(result["errNum"]) == 0)
            {
                var address = JsonHelper.ToObject(result["retData"].ToString());
                return WebApi.Success(address["country"].ToString() + address["province"] + address["city"]);
            }
            return WebApi.Error(data.info);
        }
    }

 

C#关于HttpClient的应用(一):获取IP所在的地理位置信息

标签:

原文地址:http://www.cnblogs.com/xuhang/p/5204965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!