|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| ASP.NET的实时天气及24小时天气预报(C#) |
|
ASP.NET的实时天气及24小时天气预报(C#) SDhLinux联盟 修改其中的url获得其他城市的天气情况 SDhLinux联盟 如广州为: SDhLinux联盟 http://weather.yahoo.com/forecast/CHXX0037_c.html SDhLinux联盟 注意仅适用于获得yahoo上的天气预报 SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 GetWeather.aspx SDhLinux联盟 ----------------------------------- SDhLinux联盟 SDhLinux联盟 <%@ Page language="c#" Codebehind="GetWeather.aspx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %> SDhLinux联盟 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > SDhLinux联盟 <HTML> SDhLinux联盟 <HEAD> SDhLinux联盟 <title>GetWeather</title> SDhLinux联盟 <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> SDhLinux联盟 <meta name="CODE_LANGUAGE" Content="C#"> SDhLinux联盟 <meta name="vs_defaultClientScript" content="JavaScript"> SDhLinux联盟 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> SDhLinux联盟 </HEAD> SDhLinux联盟 <body> SDhLinux联盟 <form id="GetWeather" method="post" runat="server"> SDhLinux联盟 <FONT face="宋体"> SDhLinux联盟 <P> SDhLinux联盟 <asp:Label id="lblWeather" runat="server">Weather</asp:Label></P> SDhLinux联盟 <P> SDhLinux联盟 <asp:Button id="btnGet" runat="server" Text="Get Weather"></asp:Button></P> SDhLinux联盟 <P> SDhLinux联盟 <asp:Label id="Weather2" runat="server">24小时天气</asp:Label></P> SDhLinux联盟 <P> SDhLinux联盟 <asp:Button id="btnGet2" runat="server" Text="天气预报"></asp:Button></P> SDhLinux联盟 </FONT> SDhLinux联盟 </form> SDhLinux联盟 </body> SDhLinux联盟 </HTML> SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 getWeather.aspx.cs SDhLinux联盟 --------------------------------------- SDhLinux联盟 SDhLinux联盟 using System; SDhLinux联盟 using System.Collections; SDhLinux联盟 using System.ComponentModel; SDhLinux联盟 using System.Data; SDhLinux联盟 using System.Drawing; SDhLinux联盟 using System.Web; SDhLinux联盟 using System.Web.SessionState; SDhLinux联盟 using System.Web.UI; SDhLinux联盟 using System.Web.UI.WebControls; SDhLinux联盟 using System.Web.UI.HtmlControls; SDhLinux联盟 using System.Net; SDhLinux联盟 using System.IO; SDhLinux联盟 SDhLinux联盟 namespace test SDhLinux联盟 { SDhLinux联盟 SDhLinux联盟 public class GetWeather : System.Web.UI.Page SDhLinux联盟 { SDhLinux联盟 protected System.Web.UI.WebControls.Label lblWeather; SDhLinux联盟 protected System.Web.UI.WebControls.Label Weather2; SDhLinux联盟 protected System.Web.UI.WebControls.Button btnGet2; SDhLinux联盟 protected System.Web.UI.WebControls.Button btnGet; SDhLinux联盟 SDhLinux联盟 private void Page_Load(object sender, System.EventArgs e) SDhLinux联盟 { SDhLinux联盟 // Put user code to initialize the page here SDhLinux联盟 } SDhLinux联盟 SDhLinux联盟 #region Web Form Designer generated code SDhLinux联盟 override protected void OnInit(EventArgs e) SDhLinux联盟 { SDhLinux联盟 // SDhLinux联盟 // CODEGEN: This call is required by the ASP.NET Web Form Designer. SDhLinux联盟 // SDhLinux联盟 InitializeComponent(); SDhLinux联盟 base.OnInit(e); SDhLinux联盟 } SDhLinux联盟 SDhLinux联盟 /// <summary> SDhLinux联盟 /// Required method for Designer support - do not modify SDhLinux联盟 /// the contents of this method with the code editor. SDhLinux联盟 /// </summary> SDhLinux联盟 private void InitializeComponent() SDhLinux联盟 { SDhLinux联盟 this.btnGet.Click += new System.EventHandler(this.btnGet_Click); SDhLinux联盟 this.btnGet2.Click += new System.EventHandler(this.btnGet2_Click); SDhLinux联盟 this.Load += new System.EventHandler(this.Page_Load); SDhLinux联盟 SDhLinux联盟 } SDhLinux联盟 #endregion SDhLinux联盟 SDhLinux联盟 private void btnGet_Click(object sender, System.EventArgs e) SDhLinux联盟 { SDhLinux联盟 SDhLinux联盟 WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html"); SDhLinux联盟 SDhLinux联盟 HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse(); SDhLinux联盟 SDhLinux联盟 string HTML =""; SDhLinux联盟 Stream s=wresp.GetResponseStream(); SDhLinux联盟 SDhLinux联盟 StreamReader objReader = new StreamReader(s); SDhLinux联盟 SDhLinux联盟 string sLine = ""; SDhLinux联盟 int i = 0; SDhLinux联盟 SDhLinux联盟 while (sLine!=null) SDhLinux联盟 { SDhLinux联盟 i++; SDhLinux联盟 sLine = objReader.ReadLine(); SDhLinux联盟 if (sLine!=null) SDhLinux联盟 HTML += sLine; SDhLinux联盟 } SDhLinux联盟 SDhLinux联盟 String temp= ""; SDhLinux联盟 int start,stop; SDhLinux联盟 SDhLinux联盟 start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length); SDhLinux联盟 SDhLinux联盟 stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length); SDhLinux联盟 SDhLinux联盟 temp = HTML.Substring(start, stop - start); SDhLinux联盟 start = temp.IndexOf("<b>"); SDhLinux联盟 stop = temp.IndexOf("</b>"); SDhLinux联盟 SDhLinux联盟 string degree = temp.Substring(start+3,stop - start -3); SDhLinux联盟 SDhLinux联盟 start = temp.IndexOf("<img"); SDhLinux联盟 stop = temp.IndexOf("</td>",start); SDhLinux联盟 SDhLinux联盟 string img = temp.Substring(start,stop - start); SDhLinux联盟 lblWeather.Text = degree + "<br>" + img; SDhLinux联盟 SDhLinux联盟 } SDhLinux联盟 SDhLinux联盟 private void btnGet2_Click(object sender, System.EventArgs e) SDhLinux联盟 { SDhLinux联盟 WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html"); SDhLinux联盟 SDhLinux联盟 HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse(); SDhLinux联盟 SDhLinux联盟 string HTML =""; SDhLinux联盟 Stream s=wresp.GetResponseStream(); SDhLinux联盟 SDhLinux联盟 StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312")); SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 string sLine = ""; SDhLinux联盟 int i = 0; SDhLinux联盟 SDhLinux联盟 while (sLine!=null) SDhLinux联盟 { SDhLinux联盟 i++; SDhLinux联盟 sLine = objReader.ReadLine(); SDhLinux联盟 if (sLine!=null) SDhLinux联盟 HTML += sLine; SDhLinux联盟 } SDhLinux联盟 SDhLinux联盟 String temp= ""; SDhLinux联盟 int start,stop; SDhLinux联盟 SDhLinux联盟 start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,HTML.Length); SDhLinux联盟 SDhLinux联盟 stop = HTML.IndexOf("</table>",start)+8; SDhLinux联盟 SDhLinux联盟 SDhLinux联盟 temp = HTML.Substring(start, stop - start); SDhLinux联盟 Weather2.Text = temp; SDhLinux联盟 } SDhLinux联盟 } SDhLinux联盟 } SDhLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|