Asp. NET real-time weather and 24-hour weather forecast

Source: Internet
Author: User
Tags modify net readline string visual studio

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


The following are the referenced contents:
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= "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
---------------------------------------

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;
}
}
}



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.