获取Ip所在城市名与详细
Posted iwaityou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取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; }
以上是关于获取Ip所在城市名与详细的主要内容,如果未能解决你的问题,请参考以下文章