1 /// <summary>
 2         /// 获取天气预报信息
 3         /// </summary>
 4         /// <returns></returns>
 5         public BaiduTQ GetWeather()
 6         {
 7             // GetCity()获得的信息解析后,填充丰台部分
 8             string url = @"http://api.map.baidu.com/telematics/v3/weather?location=丰台&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9";
 9 
10             HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
11             webRequest.Method = "GET";
12             webRequest.ContentType = "text/html";
13             
14             using(StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream()))
15             {
16                string str =  sr.ReadToEnd();
17                BaiduTQ b = JsonConvert.DeserializeObject<BaiduTQ>(str);
18 
19                return b;
20             }
21         }
获取天气预报信息
 1 public class BaiduTQ
 2     {
 3         public int error { get; set; }
 4         public string status { get; set; }
 5         public string date { get; set; }
 6         public List<BaiduResult> results { get; set; }
 7     }
 8  
 9     public class BaiduResult
10     {
11         public string currentCity { get; set; }
12         public string pm25 { get; set; }
13         public List<BaiduIndex> index { get; set; }
14         public List<BaiDuWeaterData> weather_data { get; set; }
15     }
16  
17     public class BaiduIndex
18     {
19         public string title { get; set; }
20         public string zs { get; set; }
21         public string tipt { get; set; }
22         public string des { get; set; }
23     }
24  
25     public class BaiDuWeaterData
26     {
27         public string date { get; set; }
28         public string dayPictureUrl { get; set; }
29         public string nightPictureUrl { get; set; }
30         public string weather { get; set; }
31         public string wind { get; set; }
32         public string temperature { get; set; }
33     }
BaiduTQ


 

 1  /// <summary>
 2         /// 根据ip获取城市信息
 3         /// </summary>
 4         /// <returns>ip所在城市信息</returns>
 5         private string GetCity()
 6         {
 7             IPAddress ip = this.GetIPAddress();
 8             if (ip == null) return null;
 9 
10             using (var client = new WebClient())
11             {
12                 var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
13                 client.Encoding = Encoding.UTF8;
14                 var str = client.DownloadString(url);
15 
16                 return str;
17             }
18         }
根据ip获取所在地
 1 /// <summary>
 2         /// 获取本地ip地址
 3         /// </summary>
 4         /// <returns></returns>
 5         private IPAddress GetIPAddress()
 6         {
 7             IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
 8             foreach (IPAddress ip in arrIPAddresses)
 9             {
10                 if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
11                 {
12                     return ip;
13                 }
14             }
15 
16             return null;
17         }
获取本地ip

ps:若需要获取外网ip,请参见http://www.cnblogs.com/hehexiaoxia/p/4996624.html

附录:国家气象局提供的天气预报接口及使用说明

posted on 2015-11-26 09:38  赫赫小虾  阅读(804)  评论(0编辑  收藏  举报