手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.net(c#)打造24小时天气预报及实时天气
ASP.net(c#)打造24小时天气预报及实时天气
摘要:本文详细介绍关于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(c#)打造24小时天气预报及实时天气】相关文章:

asp.net小孔子cms中的数据添加修改

Asp.net 时间操作基类(支持短日期,长日期,时间差)

ASP.NET中的URL映射技巧

ASP.net 动态加载控件时一些问题的总结

ASP.net 生成缩略图的实例源代码

ASP.NET缓存处理类实例

ASP.net 验证码实现代码(C#)

asp.net(c#)两种随机数的算法,可用抽考题

ASP.NET动态加载用户控件的实现方法

asp.net 生成静态时的过滤viewstate的实现方法

精品推荐
分类导航