This article describes in detail the real time weather and 24-hour weather forecast for ASP.net
Modify the URL to get a special article on the weather situation in other cities.
Asp. NET real-time weather and 24-hour weather Forecast (C #)
Modify the URL to get weather conditions in other cities
such as Guangzhou is:
Http://weather.yahoo.com
/forecast/chxx0037_c.html
Note only applies to get the weather forecast on Yahoo
Getweather.aspx
-----------------------------------
Copy Code code as follows:
<%@ 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= "Song Body" >
<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 hour weather </asp:Label></P>
<P>
<asp:button id= "BtnGet2" runat= "server" text= "Weather Forecast" >
</asp:Button></P>
</FONT>
</form>
</body>
</HTML>
GetWeather.aspx.cs
---------------------------------------
Copy Code code as follows:
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 the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Required to Designer support-do not modify
The contents is 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>");
Stop = temp. IndexOf ("</b>");
String degree = temp. Substring (start+3,stop-start-3);
Start = temp. IndexOf ("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;
}
}
}
Please specify the Chinazhan from the Chinese webmaster