ASP.net (c #) creates a 24-hour Weather Forecast and real-time weather

Source: Internet
Author: User

This article describes ASP. NET real-time weather and 24-hour Weather Forecast in detail.
Modify the url to obtain the topic of the article about weather conditions in other cities.


ASP. NET real-time weather and 24-hour Weather Forecast (C #)
Modify the url to obtain the weather conditions of other cities.
For example, Guangzhou:
Http://weather.yahoo.com
/Forecast/CHXX0037_c.html
Note: Only applicable to weather forecasts on yahoo
GetWeather. aspx
-----------------------------------

Copy codeThe Code is 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 = "">
<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 = "weatsp2" 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 codeThe Code is 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 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> ");
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 note

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.