-------------------------------------------------- ---------------------------- ASP.NET real-time weather and 24-hour weather forecast (C #) modified URLs to get other City weather conditions, such as Guangzhou: http://weather.yahoo.com/forecast/chxx0037_c.html Note Only for weather forecasts on Yahoo
GetWeather.aspx ---------------------------------- Weather 24 hours weather
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; use 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 design (); base.onit (e);}
/// /// Required method for Designer support - do not modify /// the contents of this method with the code editor /// 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 ("", html.length); stop = html.indexof (", 0, html.length); TEMP = Html.substring (start, stop - start); start = temp.indexof (""); stop = temp.indexof (""); String D Egree = Temp.Substring (Start 3, Stop - Start-3); start = Temp.indexof ("stop = temp.indexof (" ", string img = temp.substring (start, stop - start); lblWeather.Text = degree "" 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.GETRESPONSSSTREAM (); streamreader objreader = new streamreader (s, system.text.encoding.getencoding ("gb2312")); string sline = ""; int i = 0; while (WHILE) Sline! = null) {i ; sline = objreader.readline (); if (sline! = null) html = sline;} string temp = ""; int start, stop; start = html.indexof (", 0 , Html.length; stop = html.indexof (", start) 8; temp = html.substring (start, stop - start); weather2.text = temp;}}}