zoukankan      html  css  js  c++  java
  • .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;
            }

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

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

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

  • 相关阅读:
    我倾向于使用发布版本进行调试,而不是使用调试版本
    常见WinDbg问题及解决方案
    在崩溃转储中查找所有可能的上下文记录
    向C/C++程序员介绍Windbg 脚本
    VS 使用技巧(1)
    Windows资源监视器软件的原理
    微架构、指令集架构与汇编语言的关系
    调试寄存器 原理与使用:DR0-DR7
    如何学习调试?
    WinDbg: 执行 SOS 扩展命令 !clrstack时报错 Access violation exception (0xC0000005)
  • 原文地址:https://www.cnblogs.com/ruicky/p/2471945.html
Copyright © 2011-2022 走看看