获取Ip所在城市名与详细
//获取ip和地理信息string url = "http://pv.sohu.com/cityjson";
WebRequest wRequest = WebRequest.Create(url);
wRequest.Method = "GET";
wRequest.ContentType = "text/html;charset=UTF-8";
WebResponse wResponse = wRequest.GetResponse();
Stream stream = wResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
string str = reader.ReadToEnd(); //url返回的值
reader.Close();
wResponse.Close();
//var returnCitySN = {"cip": "113.57.68.117", "cid": "420100", "cname": "湖北省武汉市"};
// Response.Write(str);
var start = str.IndexOf('{');
var end = str.IndexOf('}');
str = str.Substring(start, (end - start) + 1);
//{"cip": "113.57.68.117", "cid": "420100", "cname": "湖北省武汉市"}
//Response.Write(str);
//湖北省武汉市
JObject jonObj = JObject.Parse(str);
label1.Text = jonObj["cname"].ToString() + " " + jonObj["cip"].ToString();
using (var client = new WebClient())
{
client.Encoding = Encoding.UTF8;
var urld = "http://ip.taobao.com/service/getIpInfo.php?ip="+jonObj["cip"].ToString();
var json = client.DownloadString(urld);
var ip = JsonConvert.DeserializeObject<TIPData>(json);
cityname.Text = ip.data.city;
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15316096/3209771
页:
[1]