• 获取指定城市代码的城市天气


    public static Weather GetCurrentWeather(string CityCode)
        {
            try
            {
                Uri myUri = new Uri("http://weather.news.qq.com/inc/ss" + CityCode + ".htm");

                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(myUri);

                myWebRequest.Method = "GET";

                HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

                StreamReader reader = new StreamReader(myWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));

                byte[] buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(reader.ReadToEnd());
                String HTML = System.Text.Encoding.GetEncoding("GB2312").GetString(buffer).Trim();
                String temp = "";
                int start, stop;

                start = HTML.IndexOf("</style>", 0, HTML.Length);

                stop = HTML.IndexOf("<script>", 0, HTML.Length);
                temp = HTML.Substring(start, stop - start);
                Regex regex = new Regex("<[^>]*>");
                string[] ss = regex.Split(temp);
                string t = "";
                foreach (string p in ss)
                    if (p.Trim().Length > 0)
                        t += p + "/";
                string[] sss = t.Split('/');
                Weather weather = new Weather();
                if (sss.Length > 0) weather.City = sss[0];
                if (sss.Length > 1) weather.TodayWeather = sss[1];
                if (sss.Length > 2) weather.WenDu = sss[2];
                if (sss.Length > 3) weather.FengLi = sss[3];
                if (sss.Length > 4) weather.ZiWaiXian = sss[4];
                if (sss.Length > 5) weather.KongQi = sss[5];
                reader.Close();
                myWebResponse.Close();
                return weather;
            }
            catch
            {
                Weather w = new Weather();
                w.City = "-1";
                return w;
            }
     }

  • 相关阅读:
    2017/07/25 工作日志
    2017/07/27 工作日志
    2017/07/31 工作日志
    2017/07/26 工作日志
    2017/07/28 工作日志
    远程客户端由于元数据地址主机名为服务器计算机名而无法解析WCF服务元数据的解决办法
    两步实现SQLSERVER版本降级
    dll版本号相同,提示加载dll失败
    silverlight登陆页面的小细节【自动设置焦点,回车登陆】
    Silverlight向aspx传值
  • 原文地址:https://www.cnblogs.com/hqbird/p/1318258.html
Copyright © 2020-2023  润新知