Esempio n. 1
0
        /// <summary>
        /// 取得贵阳天气信息
        /// </summary>
        /// <param name="code">城市代码</param>
        /// <returns></returns>
        public static WeaherObject GetWeatherInfor(String code)
        {
            WeaherObject w = null;

            try
            {
                w = (WeaherObject)HttpContext.Current.Cache["_WeaherObject"];
            }
            catch { }
            if (w != null)
            {
                return(w);
            }
            String url = "http://weather.news.qq.com/inc/07_ss" + code + ".htm";

            System.Net.WebClient client = new System.Net.WebClient();
            try
            {
                String key = client.DownloadString(url);
                w = new WeaherObject();

                //读取城市
                int i = key.IndexOf("class=\"wht1 lk37\">");
                key = key.Substring(i, key.Length - i);
                int j = key.IndexOf("</td>");
                w.City = key.Substring(0, j).Replace("class=\"wht1 lk37\">", "");

                //读取图片
                i             = key.IndexOf("<img");
                key           = key.Substring(i, key.Length - i);
                j             = key.IndexOf("</td>");
                w.WeaherImage = key.Substring(0, j);

                //读取天气信息
                i             = key.IndexOf("<div class=\"txbd\">");
                key           = key.Substring(i, key.Length - i);
                i             = key.IndexOf("</div>");
                w.WeaherInfor = key.Substring(0, i).Replace("<div class=\"txbd\">", "");
                key           = key.Substring(i, key.Length - i);

                //读取气温
                w.Temperature = key.Substring(6, key.IndexOf("</td") - 6);
                HttpContext.Current.Cache.Add("_WeaherObject", w, null, DateTime.Now.AddHours(1),
                                              Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
                return(w);
            }
            catch // (Exception err)
            {
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 取得指定城市的天气信息
        /// </summary>
        /// <param name="code">城市代码</param>
        /// <returns></returns>
        public static String GetWeather(String code)
        {
            WeaherObject w = GetWeatherInfor(code);

            if (w != null)
            {
                return(String.Format("<a href='http://weather.news.qq.com/preend.htm?dc" + code
                                     + ".htm' target='_blank'><span style='color:#333;'>{1} {0} {2}</span></a>", w.Temperature, w.City, w.WeaherInfor));
            }
            else
            {
                return("");
            }
        }