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

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;
}
}
}

 
告别 iframe 和 Js调用的天气预报,这些天气预报插件大部分都有外连接,或者弹窗程序,调用速度也比较慢,样式也是由他们定制的,也许和您的网站外观很不匹配。 API天气预报插件的优点 1.使用专业api接口,获取数据快,可直接获取三天。 2.每次打开调用页面的时候并不获取天气信息,所以天气插件不会影响到您网站的速度。 3.每10--30分钟在net后台自动获取天气信息一次,不存在手动更新,很方便。 4.没有任何外连接。完全展示大站风采。 5.样式有自己掌握,想做成啥样式的天气预报都可以(图标也可以自己换)。 6.使用XML形式储存,基本上不占用网站资源。 7.已经写成专业类库,只需要 new一个类就可以使用了,略懂皮毛的也能运用自如。 其实我不分析大家也应该能感觉得到调用其他站点天气的痛苦,呵呵,所以才有了这个插件。 演示站点:http://www.hhhjw.com/ 首页右侧,点-查看详情 本插件完全免费使用,天气插件使用客户QQ群6774488 插件资源来自于网络,若此程序伤害到您的利益,请联系QQ130944520 使用方式简述: 第一步 用记事本打开网站根目录web.config,找到<appSettings>节点,插入:<add key="StationCity" value="泉州" /> 泉州是城市名称,不要带市。 第二步 根目录Global.asax.cs文件,引入:using PowerLabs.Plug.Api 命名空间; 找到 public class Global : System.Web.HttpApplication 在括号内第一行加上 private static bool inited = false; //要加上,控制字段 private static object initLocker = new object(); //要加上,互斥锁 找到protected void Application_BeginRequest(object sender, EventArgs e) 在括号内第一行加上 //加上以下代码 if (inited == false) { #region 初始化 lock (initLocker) //lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。 { if (inited == false) { //启动天预报 WeatherXml.StartWeather(); //持续定义定时器,用来持续获取天气 WeatherXml.Init(); inited = true; } } #endregion } 以上2步已经回在您的网站Weather目录下更新了数据文件Weather.config(可以用记事本打开看) 第三步 页面调用。 任何页面才CS部分引入:using PowerLabs.Plug.Api 命名空间; 然后再人以位置加上以下代码就可以了。 WeatherModel myWeather = new WeatherModel(WeatherXml.getWeather()); 就是new一个我的天气 可以获得的字段 /// <summary> /// 省份 myWeather.Provinces /// </summary> /// <summary> /// 城市 myWeather.City /// </summary> /// <summary> /// 城市代码 myWeather.CityCode /// </summary> /// <summary> /// 城市图片名称 myWeather.CityImgName /// </summary> /// <summary> /// 接口更新时间 myWeather.ApiUpDateTime /// </summary> /// <summary> /// 今天气温 格式:13℃/25℃ myWeather.TodayTemperature /// </summary> /// <summary> /// 今天的日期和天气 格式:10月5日 晴 myWeather.TodayWeather /// </summary> /// <summary> /// 今天风向 myWeather.TodayWind /// </summary> /// <summary> /// 今天天气开始图标 myWeather.TodayStartIcon /// </summary> /// <summary> /// 今天天气结束图标 myWeather.TodayEndIcon /// </summary> /// <summary> /// 今天天气实况 myWeather.TodayTheWeather /// </summary> /// <summary> /// 今天室外各指数 myWeather.TodayOutdoor 这个是一个一位数组,不会使用看压缩包中的例子。 /// </summary> /// <summary> /// 明天气温 格式:13℃/25℃ myWeather.TomorrowTemperature /// </summary> /// <summary> /// 明天的日期和天气 格式:10月5日 晴 myWeather.TomorrowWeather /// </summary> /// <summary> /// 明天风向 myWeather.TomorrowWind /// </summary> /// <summary> /// 明天天气开始图标 myWeather.TomorrowStartIcon /// </summary> /// <summary> /// 明天天气结束图标 myWeather.TomorrowEndIcon /// </summary> /// <summary> /// 后天气温 格式:13℃/25℃ myWeather.AfterTemperature /// </summary> /// <summary> /// 后天的日期和天气 格式:10月5日 晴 myWeather.AfterWeather /// </summary> /// <summary> /// 后天风向 myWeather.AfterWind /// </summary> /// <summary> /// 后天天气开始图标 myWeather.AfterStartIcon /// </summary> /// <summary> /// 后天天气结束图标 myWeather.AfterEndIcon /// </summary> /// <summary> /// 本城市简介 myWeather.ThisCity /// </summary> 也可以在aspx文件直接调用,调用方式:获得今天气温:<%=myWeather.TodayTemperature%> 其他类似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值