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元素有如下子元素:

l  faultcode,错误代号;

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

l  faultactor

l  detail

包含错误信息的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:AuthException   xmlns: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);

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值