SOAP 概述


SOAP(Simple Object Access Protocol,简单对象访问协议)是一种标准化的通讯规范,主要用于 Web 服务(Web Service)。SOAP 是为了简化网页服务(Web Server)。从 XML 提取数据时,无需花时间去格式化页面。让不同的应用程序之间通过 HTTP 协议,以 XML 格式交换数据,这样,与编程语言、平台和硬件无关。

SOAP 由 IBM、Microsoft、UserLand 和 DevelopMentor 在 1998 年共同提出,并得到 IBM、Lotus、Compaq 等公司的支持,于 2000 年提交 W3C。目前 SOAP 1.1 版是业界标准,是第二代 XML 协定。第一代的主要代表为 XML-RPC 和 WDDX。

SOAP 的一个简单例子:假设,有一个房价的数据库,SOAP 消息参数中指定房价查询信息,Web 服务点根据该查询信息,返回一个 XML 格式信息,其中包含查询结果(如价格、位置、特点,或者其他信息)。由于 XML 数据是一种结构化文本标准,可以被第三方使用。

SOAP 传输方式


SOAP 使用因特网的应用层协议作为其传输协议。HTTP 或 SMTP 协议都可以用来传输 SOAP 消息,但由于 HTTP 在现在的因特网中工作得很好,特别是在网络防火墙下仍然正常工作,所以被广泛采纳。

SOAP 也可以在 HTTPS 上传输。

SOAP 语法规则


  • SOAP 消息必须使用 XML 编码

  • SOAP 消息必须使用 SOAP Envelope 命名空间

  • SOAP 消息必须使用 SOAP Encoding 命名空间

  • SOAP 消息不能包含 DTD 引用

  • SOAP 消息不能包含 XML 处理指令

SOAP 消息实例


请求
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<req:echoxmlns:req="http://localhost:8080/axis2/services/MyService/">
<req:category>classifieds</req:category>
</req:echo>
</soapenv:Body>
</soapenv:Envelope>
回应
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<soapenv:Header>
<wsa:ReplyTo>
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:From>
<wsa:Address>http://localhost:8080/axis2/services/MyService</wsa:Address>
</wsa:From>
<wsa:MessageID>ECE5B3F187F29D28BC11433905662036</wsa:MessageID>
</soapenv:Header>
<soapenv:Body>
<req:echoxmlns:req="http://localhost:8080/axis2/services/MyService/">
<req:category>classifieds</req:category>
</req:echo>
</soapenv:Body>
</soapenv:Envelope>



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

接口转发实例:

package cn.gzydt.server;

import javax.xml.ws.Endpoint;

import cn.gzydt.service.MySecondServiceImpl;
import cn.gzydt.service.MyServiceImpl;

public class Server
{
   public static void main(String[] args)
   {
       String address = "http://192.168.10.106:8889/myservice";
       Endpoint.publish(address, new MyServiceImpl());
   }
}




WSDL文件:


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://service.gzydt.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:add>
<arg0>11</arg0>
<arg1>22</arg1>
</q0:add>
</soapenv:Body>
</soapenv:Envelope>  


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


package cn.gzydt.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import cn.gzydt.service.MySecondService;
import cn.gzydt.service.MyService;

public class Client
{
   public static void main(String[] args)
   {
       try
       {
           URL url = new URL(" http://192.168.10.49:9999/102020607");//替换为系统生成的url
           QName sname = new QName("http://service.gzydt.cn/", "MyServiceImplService");
           Service service = Service.create(url, sname);
           MyService ms = service.getPort(MyService.class);
           System.out.println("12+33=" + ms.add(12,33));

       }
       catch (MalformedURLException e)
       {
           e.printStackTrace();
       }
   }
}