繁体中文
设为首页
加入收藏
当前位置:网络编程首页 >> .Net编程 >> ASP.NET的实时天气及24小时天气预报

ASP.NET的实时天气及24小时天气预报

2007-12-09 15:30:02  作者:  来源:  浏览次数:642  文字大小:【】【】【

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

如广州为:

http://weather.yahoo.com/forecast/CHXX0037_c.html

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

  GetWeather.a x
  -----------------------------------
 
  <%@ Page language="c#" Codebehind="GetWeather.a x.c quot; AutoEventWireup="false" Inherits="test.GetWeather" %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Tra itional//E quot; >
  <HTML>
  <HEAD>
  <title>GetWeather</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0" gt;
  <meta name="CODE_LANGUAGE" Content="C#" gt;
  <meta name="vs_defaultClientScript" content="JavaScript" gt;
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellise e/ie5" gt;
  </HEAD>
  < ody>
  <form id="GetWeather" method=" ost" runat=" erver" gt;
  <FONT face="宋体" gt;
  < gt;
  <a :Label id="lblWeather" runat=" erver" gt;Weather</a :Label> lt;/ gt;
  < gt;
  <a :Button id=" tnGet" runat=" erver" Text="Get Weather" gt lt;/a :Butto gt lt;/ gt;
  < gt;
  <a :Label id="Weather2" runat=" erver" gt;24小时天气</a :Label> lt;/ gt;
  < gt;
  <a :Button id=" tnGet2" runat=" erver" Text="天气预报" gt lt;/a :Butto gt lt;/ gt;
  </FONT>
  </form>
  </body>
  </HTML>
 
 
 
 
 
  getWeather.a x.cs
  ---------------------------------------
 
  using System;
  using System.Collectio 
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.We 
  using System.Web.Se io tate;
  using System.Web.UI;
  using System.Web.UI.WebControl 
  using System.Web.UI.HtmlControl 
  using System.Net;
  using System.IO;
 
  name ace test
  {
 
  public cla  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 call is required by the A .NET Web Form Designer.
  //
  InitializeComponent();
  base.OnInit(e);
  }
 
  /// < ummary>
  /// Required method for Designer su ort - 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);
 
  }
  #endregion
 
  private void btnGet_Click(object sender, System.EventArgs e)
  {
 
  WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html");
 
  HttpWebRe o e wre =(HttpWebRe o e)wreq.GetRe o e();
 
  string HTML =" quot 
  Stream s=wre .GetRe o eStream();
 
  StreamReader objReader = new StreamReader(s);
 
  string sLine = " quot 
  int i = 0;
 
  while (sLine!=null)
  {
  i++;
  sLine = objReader.ReadLine();
  if (sLine!=null)
  HTML += sLine;
  }
 
  String temp= " quot 
  int start,sto 
 
  start = HTML.IndexOf(" lt;!-- CURCON--> quot;,0,HTML.Length);
 
  stop = HTML.IndexOf(" lt;!-- END CURCON--> quot;,0,HTML.Length);
 
  temp = HTML.Su tring(start, stop - start);
  start = temp.IndexOf(" lt gt quot;);
  stop = temp.IndexOf(" lt;/ gt quot;);
 
  string degree = temp.Su tring(start+3,stop - start -3);
 
  start = temp.IndexOf(" lt;img");
  stop = temp.IndexOf(" lt;/td> quot;,start);
 
  string img = temp.Su tring(start,stop - start);
  lblWeather.Text = degree + " lt r> quot; + img;
 
  }
 
  private void btnGet2_Click(object sender, System.EventArgs e)
  {
  WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");
 
  HttpWebRe o e wre =(HttpWebRe o e)wreq.GetRe o e();
 
  string HTML =" quot 
  Stream s=wre .GetRe o eStream();
 
  StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
 
 
  string sLine = " quot 
  int i = 0;
 
  while (sLine!=null)
  {
  i++;
  sLine = objReader.ReadLine();
  if (sLine!=null)
  HTML += sLine;
  }
 
  String temp= " quot 
  int start,sto 
 
  start = HTML.IndexOf(" lt;table border=0 cellpadding=2 cell acing=1 bgcolor=9999cc width=\"85%\" gt quot;,0,HTML.Length);
 
  stop = HTML.IndexOf(" lt;/table> quot;,start)+8;
 
 
  temp = HTML.Su tring(start, stop - start);
  Weather2.Text = tem 
  }
  }
  }

责任编辑:


相关文章
 

最新文章

更多

· ASP.NET立即上手教程(12)
· ASP.NET立即上手教程(13)
· ASP.NET立即上手教程(14)
· .NET环境下几种不同的邮...
· Repeater控件分页例子
· 从文本文件读取行信息
· Asp.Net 2.0数据库基本操...
· url传递中文的解决方案
· 如何实现无刷新的Dropdo...
· 将非模态对话框显示为模...

推荐文章

更多

· ASP.NET立即上手教程(12)
· ASP.NET立即上手教程(13)
· ASP.NET立即上手教程(14)
· .NET环境下几种不同的邮...
· Repeater控件分页例子
· 从文本文件读取行信息
· Asp.Net 2.0数据库基本操...
· url传递中文的解决方案
· 如何实现无刷新的Dropdo...
· 将非模态对话框显示为模...

热点文章

更多