ASP.net(c#)打造24小时天气预报及实时天气
ASP.net(c#)打造24小时天气预报及实时天气
发布时间:2016-12-29 来源:查字典编辑
摘要:本文详细介绍关于ASP.NET的实时天气及24小时天气预报修改其中的url获得其他城市的天气情况的文章专题。ASP.NET的实时天气及24小...

本文详细介绍关于ASP.NET的实时天气及24小时天气预报

修改其中的url获得其他城市的天气情况的文章专题。

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

修改其中的url获得其他城市的天气情况

如广州为:

http://weather.yahoo.com

/forecast/CHXX0037_c.html

注意仅适用于获得yahoo上的天气预报

GetWeather.aspx

-----------------------------------

复制代码 代码如下:<%@Pagelanguage="c#"Codebehind="GetWeather.aspx.cs"AutoEventWireup="false"Inherits="test.GetWeather"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">

<HTML>

<HEAD>

<title>GetWeather</title>

<metaname="GENERATOR"Content="MicrosoftVisualStudio7.0">

<metaname="CODE_LANGUAGE"Content="C#">

<metaname="vs_defaultClientScript"content="JavaScript">

<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>

<body>

<formid="GetWeather"method="post"runat="server">

<FONTface="宋体">

<P>

<asp:Labelid="lblWeather"runat="server">Weather</asp:Label></P>

<P>

<asp:Buttonid="btnGet"runat="server"Text="GetWeather">

</asp:Button></P>

<P>

<asp:Labelid="Weather2"runat="server">24小时天气</asp:Label></P>

<P>

<asp:Buttonid="btnGet2"runat="server"Text="天气预报">

</asp:Button></P>

</FONT>

</form>

</body>

</HTML>

getWeather.aspx.cs

---------------------------------------

复制代码 代码如下:usingSystem;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Web;

usingSystem.Web.SessionState;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.HtmlControls;

usingSystem.Net;

usingSystem.IO;

namespacetest

{

publicclassGetWeather:System.Web.UI.Page

{

protectedSystem.Web.UI.WebControls.LabellblWeather;

protectedSystem.Web.UI.WebControls.LabelWeather2;

protectedSystem.Web.UI.WebControls.ButtonbtnGet2;

protectedSystem.Web.UI.WebControls.ButtonbtnGet;

privatevoidPage_Load(objectsender,System.EventArgse)

{

//Putusercodetoinitializethepagehere

}

#regionWebFormDesignergeneratedcode

overrideprotectedvoidOnInit(EventArgse)

{

//

//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.

//

InitializeComponent();

base.OnInit(e);

}

///<summary>

///RequiredmethodforDesignersupport-donotmodify

///thecontentsofthismethodwiththecodeeditor.

///</summary>

privatevoidInitializeComponent()

{

this.btnGet.Click+=newSystem.EventHandler(this.btnGet_Click);

this.btnGet2.Click+=newSystem.EventHandler(this.btnGet2_Click);

this.Load+=newSystem.EventHandler(this.Page_Load);

}

#endregion

privatevoidbtnGet_Click(objectsender,System.EventArgse)

{

WebRequestwreq=WebRequest.Create

("http://weather.yahoo.com/forecast/CHXX0037_c.html");

HttpWebResponsewresp=(HttpWebResponse)wreq.GetResponse();

stringHTML="";

Streams=wresp.GetResponseStream();

StreamReaderobjReader=newStreamReader(s);

stringsLine="";

inti=0;

while(sLine!=null)

{

i++;

sLine=objReader.ReadLine();

if(sLine!=null)

HTML+=sLine;

}

Stringtemp="";

intstart,stop;

start=HTML.IndexOf("<>",0,HTML.Length);

stop=HTML.IndexOf("<>",0,HTML.Length);

temp=HTML.Substring(start,stop-start);

start=temp.IndexOf("<b>");

stop=temp.IndexOf("</b>");

stringdegree=temp.Substring(start+3,stop-start-3);

start=temp.IndexOf("<img");

stop=temp.IndexOf("</td>",start);

stringimg=temp.Substring(start,stop-start);

lblWeather.Text=degree+"<br>"+img;

}

privatevoidbtnGet2_Click(objectsender,System.EventArgse)

{

WebRequestwreq=WebRequest.Create

("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");

HttpWebResponsewresp=(HttpWebResponse)wreq.GetResponse();

stringHTML="";

Streams=wresp.GetResponseStream();

StreamReaderobjReader=newStreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));

stringsLine="";

inti=0;

while(sLine!=null)

{

i++;

sLine=objReader.ReadLine();

if(sLine!=null)

HTML+=sLine;

}

Stringtemp="";

intstart,stop;

start=HTML.IndexOf("<tableborder=0cellpadding=2

cellspacing=1bgcolor=9999ccwidth="85%">",0,HTML.Length);

stop=HTML.IndexOf("</table>",start)+8;

temp=HTML.Substring(start,stop-start);

Weather2.Text=temp;

}

}

}请注明转自chinazhan中国站长

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新asp.net教程学习
热门asp.net教程学习
编程开发子分类