//获取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; }
原文地址:https://www.cnblogs.com/iWaitYou/p/9434044.html
时间: 2024-10-13 13:44:03