C#获取城市某一天的天气预报信息

根据城市和日期获取天气信息,未来的天气预报建议获取7天内的比较准


        public  List<WeatherRes> GetWeath( DateTime date, string address)
        {
            var ReqUrl = "http://d1.weather.com.cn/calendar_new/";
            var HostUrl = "http://www.weather.com.cn/";
            var iUrl = ReqUrl + date.GDateString("yyyy") + "/" + address + "_" + date.GDateString("yyyyMM") + ".html";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("_", DateTimeHelper.GetLong(DateTime.Now, 10000).GString());
            var apiRes = GET(iUrl, dict, null, HostUrl);
            var dyic = apiRes.IndexOf("[");
            var zhyic = apiRes.LastIndexOf("]");
            apiRes = apiRes.Substring(dyic, zhyic - dyic + 1);
            var result = apiRes.ToObject<List<HistoryRes>>();
            var res = result.Select(p => new WeatherRes()
            {
                date = DateTime.ParseExact(p.date, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).GDateString("yyyy-MM-dd"),
                max = string.IsNullOrEmpty(p.max) ? p.maxobs : p.max,
                maxobs = string.IsNullOrEmpty(p.maxobs) ? p.max : p.maxobs,
                min = string.IsNullOrEmpty(p.min) ? p.minobs : p.min,
                minobs = string.IsNullOrEmpty(p.minobs) ? p.min : p.minobs,
                rainobs = p.rainobs,
                tq = p.w1,
                fd = p.wd1

            }).ToList();
            res = res.Where(p => p.date.GDateTime() > DateTime.Now.AddDays(-6)).ToList();
            return res;

        }

 public static string GET(string url, Dictionary<string, string> param = null, NameValueCollection heder = null, string referer = "")
        {
            StringBuilder postData = new StringBuilder();
            if (param != null && param.Count > 0)
            {
                foreach (var p in param)
                {
                    if (postData.Length > 0)
                    {
                        postData.Append("&");
                    }
                    postData.Append(p.Key);
                    postData.Append("=");
                    postData.Append(p.Value);
                }
            }
            var postDataStr = postData.ToString();
            return GET(url, postDataStr, heder, referer);
        }
        public static string GET(string url, string queryString, NameValueCollection heder = null,string referer="")
        {
            var result = "";
            if (url.IndexOf("?") < 0)
            {
                url += "?";
            }
            if (url.IndexOf("=") > 0)
            {
                url += "&";
            }
            if (!string.IsNullOrEmpty(queryString))
            {
                url += queryString;
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//添加参数
            if (heder != null)
            {
                request.Headers.Add(heder);
            }
            if(!string.IsNullOrEmpty(referer))
            {
                request.Referer = referer;
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            try
            {
                //获取内容
                using (StreamReader reader = new StreamReader(stream))
                {
                    result = reader.ReadToEnd();
                }
            }
            finally
            {
                stream.Close();
            }
            return result;
        }

/// 返回实体 根据自己的需求定义

public class WeatherRes
    {

     


        /// <summary>
        ///  日期 2022-08-31
        /// </summary>
        public string date { get; set; }
       
        /// <summary>
        /// 预计最高气温
        /// </summary>
        public string max { get; set; }
        /// <summary>
        /// 实际最高气温
        /// </summary>
        public string maxobs { get; set; }
        /// <summary>
        /// 预计最低气温
        /// </summary>
        public string min { get; set; }
        /// <summary>
        ///  实际最低气温
        /// </summary>
        public string minobs { get; set; }
        

        /// <summary>
        ///  降雨量 单位 mm
        /// </summary>
        public string rainobs { get; set; }
        /// <summary>
        ///  天气 多云
        /// </summary>
        public string tq { get; set; }
        /// <summary>
        /// 风 微风
        /// </summary>
        public string fd { get; set; }
       
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值