根据ip地址获取城市

var ip=context.Request.UserHostAddress;

string url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
string stt = Encoding.GetEncoding("GBK").GetString(pageData).Trim();
return stt.Substring(stt.Length - 2, 2);

猜你喜欢

转载自www.cnblogs.com/mff520mff/p/9019159.html