码迷,mamicode.com
首页 > 其他好文 > 详细

获取Ip所在城市名与详细

时间:2018-08-06 23:18:54      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:详细   arp   ret   efault   tostring   城市   response   sub   tao   

//获取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所在城市名与详细

标签:详细   arp   ret   efault   tostring   城市   response   sub   tao   

原文地址:https://www.cnblogs.com/iWaitYou/p/9434044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!