根据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; } }