使用网上写好的webservice获取天气预报

这篇博客展示了如何使用预定义的Web服务来获取天气预报信息,并在网页上动态展示。用户通过省份和城市下拉列表选择,然后加载对应的天气情况,包括实况、未来三天的天气图标及温度等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用vs新建一个网站,在网站页面点击右键找到添加“web引用”, 如果使用的是vs2010以及以上版本的请点击添加-服务引用-高级-添加web引用。
2.在该页面的URL处输入 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx该链接,这是一位大神写好的获取天气的webservice,点击后面的前往按钮。
使用网上写好的webservice获取天气预报 - 不懂love - 不懂dé嗳的博客
3.可以自己更改自己喜欢的web引用名,也可以不改,然后点击添加引用。可以在浏览器中直接输入该链接,在里面可以查看webservice包含的方法以及含义,在该网页中可以下载天气图片压缩包,然后将解压的压缩包放入images文件夹下。
4.新建一个Default.aspx页面,在页面添加如下代码

<div>
省份:<asp:DropDownList ID="DDL_Province" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL_Province_SelectedIndexChanged"></asp:DropDownList>
城市:<asp:DropDownList ID="DDL_City" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL_City_SelectedIndexChanged"></asp:DropDownList>
</div>
<%if(Weather!=null&&Weather.Length>0){ %>
<div>
<ul>
<li>实况:<%=Weather[10] %></li>
<li>今天天气:<img src="images/weather/<%=Weather[8] %>" /><img src="images/weather/<%=Weather[9] %>" />气温:<%=Weather[5] %></li>
<li>明天天气:<img src="images/weather/<%=Weather[15] %>" /><img src="images/weather/<%=Weather[16] %>" />气温:<%=Weather[12] %></li>
<li>后天天气:<img src="images/weather/<%=Weather[20] %>" /><img src="images/weather/<%=Weather[21] %>" />气温:<%=Weather[17] %></li>
</ul>
</div>
<%} %>

5.在 Default.aspx.cs页面添加 cn.com.webxml.www该命名空间,该名称与前文的web引用名相同就可以了,然后代码如下:

private WeatherWebService client = new WeatherWebService();
public string[] Weather;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
getProvince();
getCity();
}
}

/// <summary>
/// 绑定省份
/// </summary>
public void getProvince()
{
string[] pro = client.getSupportProvince();
for (int i = 0; i <pro.Length; i++)
{
DDL_Province.Items.Add(new ListItem(pro[i], pro[i]));
}
}

/// <summary>
/// 绑定城市
/// </summary>
public void getCity()
{
DDL_City.Items.Clear();
string[] city = client.getSupportCity(DDL_Province.SelectedValue);
for (int i = 0; i < city.Length; i++)
{
DDL_City.Items.Add(new ListItem(city[i], city[i]));
}
getWeather();
}

/// <summary>
/// 获取该城市的天气
/// </summary>
public void getWeather()
{
int b=DDL_City.SelectedValue.IndexOf("(");
string city = DDL_City.SelectedValue.Substring(0, b-1);
Weather = client.getWeatherbyCityName(city);
}
protected void DDL_Province_SelectedIndexChanged(object sender, EventArgs e)
{
getCity();
}
protected void DDL_City_SelectedIndexChanged(object sender, EventArgs e)
{
getWeather();
}

6.然后就可以在浏览器中查看该页面:
使用网上写好的webservice获取天气预报 - 不懂love - 不懂dé嗳的博客
 

 7.当然绑定Dropdownlist也可以用 getSupportDataSet   方法获取数据集进行绑定,这里就不一一列举了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值