首先添加WebService 服务引用: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
namespace WebServiceDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//实例化服务类
WeatherWebServiceSoapClient weather = new WeatherWebServiceSoapClient();
private void Form1_Load(object sender, EventArgs e)
{
//获取省份
cbProvince.DataSource = weather.getSupportProvince();
}
private void cbProvince_SelectedIndexChanged(object sender, EventArgs e)
{
//获取城市
cbCity.DataSource = weather.getSupportCity(cbProvince.Text.Trim());
}
private void cbCity_SelectedIndexChanged(object sender, EventArgs e)
{
string cityName = cbCity.Text.Trim();
string cName = cityName.Split(new char[] { '(' })[0];
//显示天气信息
lboxMessage.DataSource = weather.getWeatherbyCityName(cName);
}
}
}
本文介绍了一个使用C#实现的简单示例,通过调用WebService接口获取指定城市的天气信息。该示例展示了如何实例化服务类、获取省份及城市列表,并根据选择的城市查询并展示天气详情。
3365

被折叠的 条评论
为什么被折叠?



