其他分享
首页 > 其他分享> > .Net 免费实况天气接口

.Net 免费实况天气接口

作者:互联网

免费实况天气接口:

https://www.tianqiapi.com/index/doc  
1   string ReqUrl =  $"https://tianqiapi.com/api?version=v6&appid=appid&appsecret=appsecret";
2   HttpWebResponse response =  HttpWebResponseUtility.CreatePostHttpResponse(ReqUrl, " ", null, null, Encoding.UTF8,  null);
3   Stream resStream = response.GetResponseStream();
4   StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
5   string htmlCode = sr.ReadToEnd();//获取返回JSON
6   var todayweather = JsonConvert.DeserializeObject<WeatherViewModel>(htmlCode);

 

        附:HttpWebResponseUtility
 1 public static class HttpWebResponseUtility
 2     {
 3         private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE  6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
 4       
 5          
 6         public static HttpWebResponse CreatePostHttpResponse(string url, string  parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection  cookies)
 7         {
 8             if (string.IsNullOrEmpty(url))
 9             {
10                 throw new ArgumentNullException("url");
11             }
12             if (requestEncoding == null)
13             {
14                 throw new ArgumentNullException("requestEncoding");
15             }
16             HttpWebRequest request = null;
17             //如果是发送HTTPS请求  
18             if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
19             {
20                 ServicePointManager.ServerCertificateValidationCallback = new  RemoteCertificateValidationCallback(CheckValidationResult);
21                 request = WebRequest.Create(url) as HttpWebRequest;
22                 request.ProtocolVersion = HttpVersion.Version10;
23             }
24             else
25             {
26                 request = WebRequest.Create(url) as HttpWebRequest;
27             }
28             request.Method = "POST";
29             request.ContentType = "application/x-www-form-urlencoded";
30             if (!string.IsNullOrEmpty(userAgent))
31             {
32                 request.UserAgent = userAgent;
33             }
34             else
35             {
36                 request.UserAgent = DefaultUserAgent;
37             }
38             if (timeout.HasValue)
39             {
40                 request.Timeout = timeout.Value;
41             }
42             if (cookies != null)
43             {
44                 request.CookieContainer = new CookieContainer();
45                 request.CookieContainer.Add(cookies);
46             }
47             //如果需要POST数据  
48             if (parameters == null || parameters != "")
49             {
50                 byte[] data = requestEncoding.GetBytes(parameters);
51                 using (Stream stream = request.GetRequestStream())
52                 {
53                     stream.Write(data, 0, data.Length);
54                 }
55             }
56             return request.GetResponse() as HttpWebResponse;
57         }
58         private static bool CheckValidationResult(object sender, X509Certificate  certificate, X509Chain chain, SslPolicyErrors errors)
59         {
60             return true; //总是接受  
61         }
62     }

 

  WeatherViewModel类:
 1  public class WeatherViewModel
 2     {
 3         public string cityid { get; set; }
 4         public string date { get; set; }
 5         public string week { get; set; }
 6         public string update_time { get; set; }
 7         public string city { get; set; }
 8         public string cityEn { get; set; }
 9         public string country { get; set; }
10         public string countryEn { get; set; }
11         public string wea { get; set; }
12         public string wea_img { get; set; }
13         public string tem { get; set; }
14         public string tem1 { get; set; }
15         public string tem2 { get; set; }
16         public string win { get; set; }
17         public string win_speed { get; set; }
18         public string win_meter { get; set; }
19         public string humidity { get; set; }
20         public string visibility { get; set; }
21         public string pressure { get; set; }
22         public string air { get; set; }
23         public string air_pm25 { get; set; }
24         public string air_level { get; set; }
25         public string air_tips { get; set; }
26         public Alarm alarm { get; set; }
27         public class Alarm
28         {
29             public string alarm_type { get; set; }
30             public string alarm_level { get; set; }
31             public string alarm_content { get; set; }
32         }
33     }

 

 

标签:set,string,get,request,实况,接口,Net,null,public
来源: https://www.cnblogs.com/ywkcode/p/12555983.html