当前位置:首页 > ASP.NET的实时天气及24小时天气预报(C#)

ASP.NET的实时天气及24小时天气预报(C#)

点击次数:811  更新日期:2010-12-29
\n

asp.net的实时天气及24小时天气预报(c#) 修改其中的url获得其他城市的天气情况
如广州为:
http://weather.yahoo.com/forecast/chxx0037_c.html
注意仅适用于获得yahoo上的天气预报


\n

getweather.aspx
———————————–


\n

<%@ 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>


\n

getweather.aspx.cs
—————————————


\n

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;


\n

namespace test
{


\n

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;


\n

private void page_load(object sender, system.eventargs e)
{
// put user code to initialize the page here
}


\n

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


\n

/// <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);


\n

}
#endregion


\n

private void btnget_click(object sender, system.eventargs e)
{


\n

webrequest wreq=webrequest.create(“http://weather.yahoo.com/forecast/chxx0037_c.html”);


\n

httpwebresponse wresp=(httpwebresponse)wreq.getresponse();


\n

string html =”";
stream s=wresp.getresponsestream();


\n

streamreader objreader = new streamreader(s);


\n

string sline = “”;
int i = 0;


\n

while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}


\n

string temp= “”;
int start,stop;


\n

start = html.indexof(“<!– curcon–>”,0,html.length);


\n

stop = html.indexof(“<!– end curcon–>”,0,html.length);


\n

temp = html.substring(start, stop – start);
start = temp.indexof(“<b>”);
stop = temp.indexof(“</b>”);


\n

string degree = temp.substring(start+3,stop – start -3);


\n

start = temp.indexof(“<img”);
stop = temp.indexof(“</td>”,start);


\n

string img = temp.substring(start,stop – start);
lblweather.text = degree + “<br>” + img;


\n

}


\n

private void btnget2_click(object sender, system.eventargs e)
{
webrequest wreq=webrequest.create(“http://cn.weather.yahoo.com/chxx/chxx0037/index_c.html”);


\n

httpwebresponse wresp=(httpwebresponse)wreq.getresponse();


\n

string html =”";
stream s=wresp.getresponsestream();


\n

streamreader objreader = new streamreader(s,system.text.encoding.getencoding(“gb2312″));


\n


string sline = “”;
int i = 0;


\n

while (sline!=null)
{
i++;
sline = objreader.readline();
if (sline!=null)
html += sline;
}


\n

string temp= “”;
int start,stop;


\n

start = html.indexof(“<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\\”85%\\”>”,0,html.length);


\n

stop = html.indexof(“</table>”,start)+8;


\n


temp = html.substring(start, stop – start);
weather2.text = temp;
}
}
}

\n