您的位置:首页 > 编程语言 > C#

C#调用WebService实现天气预报

2013-10-12 14:08 344 查看
本文使用Winform (C#)调用互联网上公开的WebServices(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)来实现天气预报,该天气预报
Web 服务,数据来源于中国气象局 http://www.cma.gov.cn/ ,数据每2.5小时左右自动更新一次,准确可靠。包括
340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。

程序效果:





实现步骤:

1、引入Web服务。在VS中项目上右击→添加服务引用。





2、在弹出的添加服务引用窗口,录入web服务地址和引用后的命名空间。





3、布置winform窗体界面,实现代码。核心代码如下:

private void button1_Click_1(object sender, EventArgs e)
{
Weather.WeatherWebServiceSoapClient w = new Weather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
//把webservice当做一个类来操作
string[] s = new string[23];//声明string数组存放返回结果
string city = this.textBox1.Text.Trim();//获得文本框录入的查询城市
s = w.getWeatherbyCityName(city);
//以文本框内容为变量实现方法getWeatherbyCityName
if (s[8] == "")
{
MessageBox.Show("暂时不支持您查询的城市");
}
else
{
pictureBox1.Image = Image.FromFile(@"d:\image\" + s[8] + "");
this.label4.Text =s[1]+" "+s[6];
textBox2.Text = s[10];
}

}


4、天气图标可至【http://www.webxml.com.cn/images/weather.zip】下载。

5、Web服务的各方法参数直接访问【http://www.webxml.com.cn/WebServices/WeatherWebService.asmx】查询,从而实现其它丰富功能,如未来天气预报等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: