.Net 得到星期 和 天气预报

得到星期的效果: 如  星期四

代码实现如下:

View Code
 1 private void Window_Loaded(object sender, RoutedEventArgs e)
 2         {
 3             this.label1.Content = DateTime.Now.ToLongDateString().ToString();
 4 
 5             string thisdl = DateTime.Today.DayOfWeek.ToString();
 6             string thisweek = "";
 7             switch (thisdl)
 8             {
 9                 case "Monday":
10                     thisweek = "星期一";
11                     break;
12                 case "Tuesday":
13                     thisweek = "星期二";
14                     break;
15                 case "Wednesday":
16                     thisweek = "星期三";
17                     break;
18                 case "Thursday":
19                     thisweek = "星期四";
20                     break;
21                 case "Friday":
22                     thisweek = "星期五";
23                     break;
24                 case "Saturday":
25                     thisweek = "星期六";
26                     break;
27                 case "Sunday":
28                     thisweek = "星期日";
29                     break;
30             }
31             label2.Content = thisweek;
32        
33         }

得到 天气预报的效果如:天气:晴转多云,24℃~13℃

代码实现如下:

View Code
public static string GetWeatherByCity(string City)
        {
            string Html = "";       //返回来天气预报源码
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = string.Format("city=" + City);
            byte[] data = encoding.GetBytes(postData);
            string weather = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" +City + "&f=1&dpc=1");
                request.Method = "Get";
                request.ContentType = "application/x-www-form-urlencoded ";
                WebResponse response = request.GetResponse();
                Stream s = response.GetResponseStream();
                StreamReader sr = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
                Html = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
            }
            //去除多余代码提高效率
            int count = Html.Length;
            int starIndex = Html.IndexOf("<li class=\"li_04\">", 0, count);
            int endIndex = Html.IndexOf("</li>", starIndex);
            Html = Html.Substring(starIndex, endIndex - starIndex);

            try
            {
                #region 得到天气
                int WeatherStartIndex = Html.IndexOf("天气:", 0);
                int WeatherEndIndex = Html.IndexOf(",风力", 0);
                weather = Html.Substring(WeatherStartIndex, WeatherEndIndex - WeatherStartIndex);

                #endregion
            }
            catch (Exception err)
            {

                throw new Exception(err.Message);
            }
            return weather;
        }

  天气预报的实现思路: 读取那个页面的源码  然后在源码中截取自己需要展示的地方。

    注:网上有很多的实现天气预报的方式 ,这只是其中一种 还有引用服务的!

大家可以去借鉴一下!这个只是作为笔记来记录、、

posted @ 2012-04-26 19:49  Ruicky  阅读(519)  评论(0编辑  收藏  举报