基于SOAP的WebService的调用原理

版权声明: 本文由 一只博客 发表于 bloghome博客javascript

文章连接: https://www.bloghome.com.cn/user/cnn237111java

SOAP的概念应该不是什么新鲜事物了。简单的说,SOAP是以把数据以XML的方式组合起来,而后经过HTTP协议(也能够是其它协议,可是一般老是用http协议)和远程服务进行通讯的一个标准,也能够把它认为是一个中间件,起着沟通桥梁的做用。由于当全部的服务都使用同一种标准,那么沟通就比较容易了。jquery

固然不得不认可,SOAP格式的报文内容冗余,而且依赖于定义好的XML schemas,对于手工建立SOAP报文十分复杂,须要借助一些工具来简化工做。所以愈来愈多的Web服务倾向于使用Restful风格的WebService。web

根据SOAP的协议,只须要发送有效的SOAP消息到服务端,就能实现通讯。那么如何生成有效的SOAP消息?SOAP官方文档详细说明了SOAP的格式,但官方文档很长,通常人没耐心去读完,大多时候仅仅在须要的时候去查一下,也能够去http://w3school.com.cn/soap/soap_syntax.asp学习一下简化版的。这里介绍一个工具,SoapUI,能够去它官网http://www.soapui.org/下载,它能够经过WSDL生成请求的SOAP消息格式。ajax

ASP.NET中,采用向导建立的Web服务,是SOAP风格的。假设咱们建立一个web服务。使用向导,完成以下的代码:c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int Add(int a, int b)
        {
            return a + b;
        }
        /// <summary>
        /// 把公制单位的汽车转化成以英制单位表示的汽车
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [WebMethod]
        public car ChangeCarUnit(car s)
        {
            s.length = s.length * 3.3m;
            s.width = s.width * 3.3m;
            s.weight = s.width * 2.2m;
            return s;
        }
    }
    public class car
    {
        public string model = "";
        public decimal length = 0;
        public decimal width = 0;
        public decimal weight = 0;
    }
}

方法都写在WebService1.asmx文件中,经过web服务的WSDL,能够获得它的SOAP消息格式。这两采用SoapUI输入指定的WSDL文件,便可以自动生成SOAP消息格式。浏览器

clipboard[26]

注意:在ASP.net中,能够经过访问WebService1.asmx而且输入查询字符串?WSDL,即在IE浏览器中输入WebService1.asmx?wsdl就能够得到WSDL文件。服务器

还有一点要注意的是,微软生成的WSDL文件,有2个binding,分别表示soap1.1和soap1.2,它都支持。框架

clipboard[27]

所以在SoapUI中能够看到2个不一样的WebService接口,实际上是大同小异的。ide

clipboard[28]

双击Add的Request,就能够获得SOAP消息格式了,其中的问号能够输入指定的值,而后点击执行按钮,就又能够获得响应的SOAP消息格式了。

clipboard[29]

经过SoapUI生成SOAP消息后,就能够本身构造SOAP消息来调用SOAP风格的WebService,所以,只要解决如何生成请求的SOAP消息,咱们甚至能够本身实现一个Web服务调用框架,不管是基于PHP,ASP.net,仍是javascript。

-----------------------------------------------------------------------

下面的演示如何在javascript中发送SOAP。

一下代码调用以前WebService1中的方法ChangeCarUnit,这个方法把汽车参数从公制为单位的转换成英制单位。

首先手动经过SoapUI获取SOAP消息格式。生成并补充数据,获得以下的格式

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:ChangeCarUnit>
         <!--Optional:-->
         <tem:s>
            <!--Optional:-->
            <tem:model>Passat</tem:model>
            <tem:length>4.87</tem:length>
            <tem:width>1.834</tem:width>
            <tem:weight>1435</tem:weight>
         </tem:s>
      </tem:ChangeCarUnit>
   </soapenv:Body>
</soapenv:Envelope>

所以只需将这串xml发送到webservice既可。

经过jquery ajax实现。

代码以下:

<script type="text/javascript">
    $(function () {
        $("#btnclick").click(function () {
            var soapmessage = "";
            soapmessage += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">';
            soapmessage += '<soapenv:Header/>';
            soapmessage += '<soapenv:Body>';
            soapmessage += '<tem:ChangeCarUnit>';
            soapmessage += ' <!--Optional:-->';
            soapmessage += ' <tem:s>';
            soapmessage += ' <!--Optional:-->';
            soapmessage += ' <tem:model>Passat</tem:model>';
            soapmessage += ' <tem:length>4.87</tem:length>';
            soapmessage += ' <tem:width>1.834</tem:width>';
            soapmessage += ' <tem:weight>1435</tem:weight>';
            soapmessage += ' </tem:s>';
            soapmessage += '</tem:ChangeCarUnit>';
            soapmessage += ' </soapenv:Body>';
            soapmessage += '</soapenv:Envelope>';
            var option = {
                url: 'http://localhost:28689/WebService1.asmx',
                type: 'POST',
                contentType: 'text/xml',
                success: function (result) {
                    alert(result.documentElement.textContent);
                },
                data: soapmessage
            };
            $.ajax(option);
        });
    });
</script>
<input value='click' type="button" id="btnclick" />

点击按钮后,就会将soap消息post到web服务器,而且获得返回消息,返回消息也是基于XML的文本。

经过上面的实例,咱们能够经过编写专用的工具来生成SOAP消息,经过封装后,再经过POST方式(好比c#中的HttpWebRequest)来实现webserverice的调用。

相关文章
相关标签/搜索