• 根据ip地址获得国家和城市(C#)


            /// <summary>
            /// get country and city
            /// </summary>
            /// <param name="ip"></param>
            /// <returns></returns>
            public static List<string> IPGetCity(string ip)
            {
                var country = "";
                var city = "";
                var list = new List<string>();
                try
                {
                    WebClient MyWebClient = new WebClient();
                    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据  
                    Byte[] pageData = MyWebClient.DownloadData("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip); //从指定网站下载数据  
                    string pageHtml = Encoding.Default.GetString(pageData);
                    dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(pageHtml);
                    if (pageHtml.Contains("country")&& pageHtml.Contains("city"))
                    {
                        country = data.country;
                        city = data.city;
                    }
                    list.Add(country);
                    list.Add(city);
                    return list;
                }
                catch (Exception ex)
                {
                    list.Add(country);
                    list.Add(city);
                    return list;
                }
            }
  • 相关阅读:
    读写INI配置文件
    log4net自动邮件
    C#往SQLServer中插入大数据
    C#反射
    正则表达式
    收发邮件
    读写文本
    Selenium—选择框的相关操作(单选框、多选框、复选框、下拉框)
    Selenium—iframe的操作
    Selenium—web元素的操作
  • 原文地址:https://www.cnblogs.com/dayang12525/p/7308447.html
Copyright © 2020-2023  润新知