简单的webservice调用(天气预报)

1.新建一个项目

2.邮件点击解决方案下的网站目录,选择“添加web引用”,输入web服务的网址,必须是以 asmx结尾的,且不带参数,(这里以http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

为例)然后前往---

3.在web引用名  中的就是命名空间,有的是自动填好的,有的需要自己填写,然后在 程序里添加引用就行了·· 

4。在程序里  添加 using cn.com.webxml.webservice;

这样就可以了 实例化然后调用方法了,对了,还要看web服务商的“接口帮助文档”来查看有哪些方法可以调用

接下来

if (!Page.IsPostBack)
        {
            cn.com.webxml.webservice.WeatherWS weather = new WeatherWS(); //实例化一个类
            DataSet ds = weather.getRegionDataset();//调用getRegionDataset() 方法,返回的是一个dataset
            DropDownList1.DataTextField = "RegionName";// 字段名 这里获取的省份
             DropDownList1.DataValueField = "RegionID";//字段名
            DropDownList1.DataSource = ds.Tables[0]; 绑定到dropdownlist 上
            DropDownList1.DataBind();
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
          
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cn.com.webxml.webservice.WeatherWS weather = new WeatherWS();
        //DataSet ds = weather.getRegionDataset();
        //DropDownList1.DataTextField = "RegionName";
        //DropDownList1.DataValueField = "RegionID";
        //DropDownList1.DataSource = ds.Tables[0];
        //DropDownList1.DataBind();
        //GridView1.DataSource = ds.Tables[0].DefaultView;
        //GridView1.DataBind();

       string[] arr = weather.getWeather(DropDownList2.SelectedItem.Text,"");//调用GetWeather 方法 参数1:城市,或者城市的ID,参数2:用户名,免费用户为空,这个方法返回的是string类型的一维数组
       Response.Write(arr[7].ToString() + @"img\weather\weather" + arr[10].ToString()); //根据开发文档,调去需要的数据
       Image1.ImageUrl =@"img\weather\weather\"+arr[10].ToString(); //天气图片,下载到本地,然后调用
       Image2.ImageUrl = @"img\weather\weather\" + arr[11].ToString();
       Image1.ToolTip = arr[7].ToString();

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        cn.com.webxml.webservice.WeatherWS weather = new WeatherWS();//根据省份获取,这个省份里的城市,然后绑定到dropdownlist2上
        Label1.Text = DropDownList1.SelectedItem.Text.ToString();
        DropDownList2.DataSource = weather.getSupportCityDataset(DropDownList1.SelectedValue.ToString());
        DropDownList2.DataTextField = "CityName";
        DropDownList2.DataValueField = "CityID";
        DropDownList2.DataBind();
    }

原文地址:https://www.cnblogs.com/tiancai/p/2175516.html