评论

收藏

[R语言] 获取Ip所在城市名与详细

编程语言 编程语言 发布于:2021-07-30 19:07 | 阅读数:246 | 评论:0

//获取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;
}


关注下面的标签,发现更多相似文章