您现在的位置: 天下网吧 >> 网吧天地 >> 天下码农 >> 微信小程序 >> 正文

实时天气及24小时天气预报

2010-12-28vczx佚名

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


GetWeather.aspx
-----------------------------------
Weather
24小时天气


getWeather.aspx.cs
---------------------------------------
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;


namespace test
{
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;


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 ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}


///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
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");
HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
string HTML ="";
Stream s=wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
String temp= "";
int start,stop;
start = HTML.IndexOf("",0,HTML.Length);
stop = HTML.IndexOf("",0,HTML.Length);
temp = HTML.Substring(start, stop - start);
start = temp.IndexOf("");
stop = temp.IndexOf("");
string degree = temp.Substring(start+3,stop - start -3);
start = temp.IndexOf("stop = temp.IndexOf("",start);
string img = temp.Substring(start,stop - start);
lblWeather.Text = degree + "" + img;
}
private void btnGet2_Click(object sender, System.EventArgs e)
{
WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");


HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();


string HTML ="";
Stream s=wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}


String temp= "";
int start,stop;
start = HTML.IndexOf("",0,HTML.Length);
stop = HTML.IndexOf(" ",start)+8;
temp = HTML.Substring(start, stop - start);
Weather2.Text = temp;
}
}
}

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛 https://bbs.txwb.com

关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩

本文来源:vczx 作者:佚名

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系邮箱:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧 网吧天下