程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET的實時天氣及24小時天氣預報(C#)

ASP.NET的實時天氣及24小時天氣預報(C#)

編輯:.NET實例教程
ASP.Net的實時天氣及24小時天氣預報(C#) 修改其中的url獲得其他城市的天氣情況
如廣州為:
http://weather.yahoo.com/forecast/CHXX0037_c.Html
注意僅適用於獲得yahoo上的天氣預報


GetWeather.ASPx
-----------------------------------

<%@ Page language="c#" Codebehind="GetWeather.ASPx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
    <HEAD>
        <title>GetWeather</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClIEntScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
    </HEAD>
    <body>
        <form id="GetWeather" method="post" runat="server">
            <FONT face="宋體">
                <P>
                    <asp:Label id="lblWeather" runat="server">Weather</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet" runat="server" Text="Get Weather"></ASP:Button></P>
                <P>
                    <asp:Label id="Weather2" runat="server">24小時天氣</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet2" runat="server" Text="天氣預報"></ASP:Button></P>
            </FONT>
        </form>
    </body>
</Html>

 

 

getWeather.ASPx.cs
---------------------------------------

using System;
using System.CollectioASP.Net的實時天氣及24小時天氣預報(C#) 修改其中的url獲得其他城市的天氣情況
如廣州為:
http://weather.yahoo.com/forecast/CHXX0037_c.Html
注意僅適用於獲得yahoo上的天氣預報


GetWeather.ASPx
-----------------------------------

<%@ Page language="c#" Codebehind="GetWeather.ASPx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
    <HEAD>
        <title>GetWeather</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClIEntScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
    </HEAD>
    <body>
        <form id="GetWeather" method="post" runat="server">
            <FONT face="宋體">
                <P>
                    <asp:Label id="lblWeather" runat="server">Weather</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet" runat="server" Text="Get Weather"></ASP:Button></P>
                <P>
                    <asp:Label id="Weather2" runat="server">24小時天氣</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet2" runat="server" Text="天氣預報"></ASP:Button></P>
            </FONT>
        </form>
    </body>
</Html>

 

 

getWeather.ASPx.cs
---------------------------------------

using System;
using System.CollectioASP.Net的實時天氣及24小時天氣預報(C#) 修改其中的url獲得其他城市的天氣情況
如廣州為:
http://weather.yahoo.com/forecast/CHXX0037_c.Html
注意僅適用於獲得yahoo上的天氣預報


GetWeather.ASPx
-----------------------------------

<%@ Page language="c#" Codebehind="GetWeather.ASPx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
    <HEAD>
        <title>GetWeather</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClIEntScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
    </HEAD>
    <body>
        <form id="GetWeather" method="post" runat="server">
            <FONT face="宋體">
                <P>
                    <asp:Label id="lblWeather" runat="server">Weather</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet" runat="server" Text="Get Weather"></ASP:Button></P>
                <P>
                    <asp:Label id="Weather2" runat="server">24小時天氣</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet2" runat="server" Text="天氣預報"></ASP:Button></P>
            </FONT>
        </form>
    </body>
</Html>

 

 

getWeather.ASPx.cs
---------------------------------------

using System;
using System.CollectioASP.Net的實時天氣及24小時天氣預報(C#) 修改其中的url獲得其他城市的天氣情況
如廣州為:
http://weather.yahoo.com/forecast/CHXX0037_c.Html
注意僅適用於獲得yahoo上的天氣預報


GetWeather.ASPx
-----------------------------------

<%@ Page language="c#" Codebehind="GetWeather.ASPx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
    <HEAD>
        <title>GetWeather</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClIEntScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
    </HEAD>
    <body>
        <form id="GetWeather" method="post" runat="server">
            <FONT face="宋體">
                <P>
                    <asp:Label id="lblWeather" runat="server">Weather</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet" runat="server" Text="Get Weather"></ASP:Button></P>
                <P>
                    <asp:Label id="Weather2" runat="server">24小時天氣</ASP:Label></P>
                <P>
                    <asp:Button id="btnGet2" runat="server" Text="天氣預報"></ASP:Button></P>
            </FONT>
        </form>
    </body>
</Html>

 

 

getWeather.ASPx.cs
---------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

namespace test
{

    public class GetWeather : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblWeather;
        protected System.Web.UI.WebControls.Label Weather2;
        protected System.Web.UI.WebControls.Button btnGet2;
        protected System.Web.UI.WebControls.Button btnGet;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.Net Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
            this.btnGet2.Click += new System.EventHandler(this.btnGet2_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion ;    private void btnGet_Click(object sender, System.EventArgs e)
        {

            WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.Html");
                
            HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
                    
            string Html ="";
            Stream s=wresp.GetResponseStream();
                
            StreamReader objReader = new StreamReader(s);

            string sLine = "";
            int i = 0;

            while (sLine!=null)
            {
                i++;
                sLine = objReader.ReadLine();
                if (sLine!=null)
                    Html += sLine;
            }
                
            String temp= "";
            int start,stop;
                            
            start = HTML.IndexOf("<!-- CURCON-->",0,Html.Length);

            stop = HTML.IndexOf("<!-- END CURCON-->",0,Html.Length);

            temp = Html.Substring(start, stop - start);
            start = temp.IndexOf("<b>");
     &nbsp;      stop = temp.IndexOf("</b>");
            
            string degree = temp.Substring(start+3,stop - start -3);

            start = temp.IndexOf("<img");
            stop = temp.IndexOf("</td>",start);

            string img = temp.Substring(start,stop - start);
            lblWeather.Text = degree + "<br>" + img;
                
        }

        private void btnGet2_Click(object sender, System.EventArgs e)
        {
            WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.Html");
                
            HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
                    
            string Html ="";
            Stream s=wresp.GetResponseStream();
                
            StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
    

            string sLine = "";
            int i = 0;

            while (sLine!=null)
            {
                i++;
                sLine = objReader.ReadLine();
                if (sLine!=null)
                    Html += sLine; 
         }
                
            String temp= "";
            int start,stop;
                            
            start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,Html.Length);

            stop = Html.IndexOf("</table>",start)+8;
            

            temp = Html.Substring(start, stop - start);
            Weather2.Text = temp;        
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved