zoukankan      html  css  js  c++  java
  • C#关于HttpClient的应用(一):获取IP所在的地理位置信息

        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);
            }
        }
  • 相关阅读:
    BZOJ 4358 坑 莫队+线段树 死T
    BZOJ 4321 DP
    两倍问题
    通宵教室
    [编程题]字符串模式匹配
    [编程题]表达式求值
    [编程题]美团骑手包裹区间分组
    1153 Decode Registration Card of PAT
    1154 Vertex Coloring
    1155 Heap Paths
  • 原文地址:https://www.cnblogs.com/xuhang/p/5204965.html
Copyright © 2011-2022 走看看