WebService 简单使用(天气查询)

 

 

首先添加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);
        }
    }
}

 

你可能感兴趣的:(webservice)