开发工具 Visual Studio
学习示例网址: https://www.cnblogs.com/zkwarrior/p/5941741.html
天气接口帮助:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
具体的返回String[23]的23个详细信息 可以在天气帮助接口里查看。
1.创建一个窗体应用
在引用处,点击 添加服务引用,然后点击 高级,然后点击添加web引用,为什么会点击web引用,而不是直接添加服务引用?
https://blog.csdn.net/u011800822/article/details/51755974 在这里有答案。
输入 : http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
使用代码如下:
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;
namespace WindowsFormsApp23 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
}
private void button1_Click(object sender, EventArgs e) {
Weather.WeatherWebService w = new Weather.WeatherWebService();
String[] s = new String[23];
String city = this.textBox1.Text.Trim(); //你想要查天气的城市名字
s = w.getWeatherbyCityName(city);
for (int i = 0; i < s.Length; i ++ ) { //看看错误情况是怎样的返回,也有23个
if (String.IsNullOrWhiteSpace(s[i])) {
s[i] = "A";
}
}
String result = String.Join("\r\n",s); //查看结果,直接显示
this.textBox1.Text = result;
}
}
}
如果点击运行,出现错误
无法加载协定为“WeatherWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
解决棒法如下: