微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

soap

soap

soap是一种基于XML的简易协议,允许程序通过HTTP来交换信息,用在分散或分布的环境中交换信息。

一条soap消息就是一个普通的xml文档,包含下列元素:

l必须的Envelope元素,标识该文档为一条soap消息;

l可选的Header元素,用于传输头信息;

l必需的Body元素,包含了所有的调用或响应信息;

l可选的Fault元素,提供有关处理错误的信息;

以上的元素均被声明为针对soap封装的认命名空间中:http://schemas.xmlsoap.org/soap/envelope/,一条soap消息如下:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:q0="www.ssl.com"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soape:Body>

<q0:add>

<a>1</a>

<b>2</b>

</q0:add>

</soape:Body>

</soape:Envelope>

1、 Header

可选的Header元素包含有关SOAP消息的应用程序相关信息,如认证、安全等。如果提供Header元素,则必须是Envelop元素的第一个子元素。

注意:所有Header元素的直接子元素必须有合格的命名空间,如下

<?xmlversion="1.0"?>

<soap:Envelope

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:q0="www.ssl.com"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soap:Header>

<m:Trans xmlns:m="http://www.ssl.com">234</m:Trans> </soap:Header>

<soap:body>

</soap:body>

</soap:Envelope>

2、 Body

Body元素是必须的,包含了发送或接受的信息。Body元素的直接子元素也必须有合格的命名空间,如下

<?xmlversion="1.0"?>

<soap:Envelope

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:q0="www.ssl.com"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soap:Body>

<m:GetPricexmlns:m="http://www.ssl.com/prices"> <m:Item>Apples</m:Item>

</m:GetPrice>

</soap:body>

</soap:Envelope>

该消息的响应消息如下:

<?xmlversion="1.0"?>

<soap:Envelope

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:q0="www.ssl.com"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soap:Body>

<m:GetPriceResponsexmlns:m="http://www.ssl.com/prices"> <m:price>3.6</m:price>

</m:GetPriceResponse>

</soap:body>

</soap:Envelope>

对于body元素的非直接子元素,在消息发送和消息响应时最好统一是否增加前缀,如不一致数据接受可能会出现错误

3、 Fault

Fault元素用于传输SOAP的错误信息。如果Fault元素存在,则必须是Body元素的子元素,在一条SOAP消息中,Fault元素只能出现一次。Fault元素有如下子元素:

lfaultcode,错误代号;

l faultString错误描述信息,最常用;

lfaultactor

ldetail

包含错误信息的SOAP消息如下

<?xml version='1.0'encoding='UTF-8'?>

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body>

<S:Faultxmlns:ns4="http://www.w3.org/2003/05/soap-envelope">

<faultcode>S:Server</faultcode>

<faultstring>没有认真信息,认证失败</faultstring>

<detail>

<ns2:AuthExceptionxmlns:ns2="http://www.ssl.com/student/">

<message>没有认真信息,认证失败</message>

</ns2:AuthException>

</detail>

</S:Fault>

</S:Body>

</S:Envelope>

4、 Binding

定义了一种使用底层传输协议来完成在节点间交换SOAP信封的约定。

5、 API

SOAP消息基本上是从发送端到接收端的单向传输,常常结合起来执行类似于请求/应答的模式。一条SOAP消息就是一个普通的XML文档,其结构如下图所示,


在JAX-WS中,一条消息对应一个SOAPMessage实例。SOAPMessage封装了对soap消息的操作。WebService的本质就是SOAP消息的发送和SOAP消息的响应,所以可以通过SOAPMessage实现对webService的调用(其他相关API请参考jaxws-api),如下:

String ns="ns;

//1. 创建服务

URL ur=new URL("wsdl-address");

QName qname=new QName(ns,"WebServiceName");

Service service=Service.create(ur,qname);

//2、创建dispatch

dispatch<SOAPMessage> dispatch=service.createdispatch(new QName(ns,"WebServicePortName"),SOAPMessage.class,Service.Mode.MESSAGE);

//3、创建SOAPMessage

SOAPMessage message=MessageFactory.newInstance().createMessage();

SOAPEnvelope envelope=message.getSOAPPart().getEnvelope();

SOAPBody body=envelope.getBody();

//4、创建qname来指定消息中传递数据

//指定那个方法

QName ename=new QName(ns,"add","nn");

SOAPBodyElement element=body.addBodyElement(ename);

element.addChildElement("a").setValue("1");

element.addChildElement("b").setValue("2");

//打印消息

message.writeto(System.out);

System.out.println("\ninvoking");

//5、通过dispatch传递消息

SOAPMessage response=dispatch.invoke(message);

response.writeto(System.out);

原文地址:https://www.jb51.cc/xml/296312.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。