zoukankan      html  css  js  c++  java
  • C#调用WebService实现天气预报

    本文使用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窗体界面,实现代码。核心代码如下:

     

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

     

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

    5、Web服务的各方法参数直接访问【http://www.webxml.com.cn/WebServices/WeatherWebService.asmx】查询,从而实现其它丰富功能,如未来天气预报等。

  • 相关阅读:
    powershell,系统学习的第一种脚本语言
    mysql的source命令
    timer--计时器
    document.write 方法
    数组去重
    Abdicate
    轮播图
    使用 margin 让div块内容居中
    模运算 NOJ 1037
    模运算 NOJ 1037
  • 原文地址:https://www.cnblogs.com/chenpan/p/3661816.html
Copyright © 2011-2022 走看看