WebService 的简单使用

简单介绍

WebService是一种跨语言,跨进程,跨机器的数据交互技术。

SOAP:简单对象访问协议,通过XML数据交互的轻量级协议,WebService就是采用的这种协议

WSDL:web服务描述语言,描述了服务API的参数以及返回结果等信息

WebService的的使用

1.首先新建一个空的web项目,然后添加“web服务”,这个文件的后缀名是asmx

2.新建一个WindowsForm项目,可以创建任何类型的项目

  private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtOne.Text) || string.IsNullOrEmpty(txtTwo.Text))
            {
                MessageBox.Show("请输入正确的数值");
            }
            else
            {
                double one = double.Parse(txtOne.Text);
                double two = double.Parse(txtTwo.Text);
                SumServiceReference.WebService1SoapClient client = new SumServiceReference.WebService1SoapClient();
                double result=  client.Add(one, two);
                txtResult.Text = result.ToString();

            }
        }

天气预报的WebServie接口的使用

1.添加天气预报的服务引用

 

 2.此时运行程序的时候会出现错误

 

因为此时需要添加Web引用,具体的操作:添加服务引用——》高级——》添加web引用

 

 3.实现的效果

4.实现的代码

 1  private void button2_Click(object sender, EventArgs e)
 2         {
 3             txtContent.Clear();
 4             if (!string.IsNullOrEmpty(txtCity.Text))
 5             {
 6                 WebWeatherService.WeatherWebService weatherService = new WebWeatherService.WeatherWebService();
 7                 string[] content=weatherService.getWeatherbyCityName(txtCity .Text);
 8                 foreach (string str in content)
 9                 {
10                     txtContent.AppendText(str + "\n");
11                 }
12             }
13         }

 5.补充

每次添加一个服务引用,都会在配置文件中,自动添加相应的endpoint节点,需要保证每个节点都是不重复的,否则将会出现访问异常。

 

转载于:https://www.cnblogs.com/XZhao/p/7298514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值