C# 调用WebService

一、

1.获取天气预报数据为例子,天气预报的WebService地址: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

二、实现步骤

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

  

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

上图 找到服务为 “WeatherWebService”

使用的源代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using WindowsFormsApp1.cn.com.webxml.www;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            WeatherWebService w = new WeatherWebService();
            string[] weatherData = new string[23];
            string city = "上海";
            weatherData = w.getWeatherbyCityName(city);
            if (weatherData[8] == "")
            {
                MessageBox.Show("暂时不支持您查询的城市");
            }
            else
            {
                label1.Text = weatherData[1] + " " + weatherData[6];
              
            }
        }
    }
}
原文地址:https://www.cnblogs.com/ike_li/p/14096313.html