现在的位置: 首页 > 综合 > 正文

使用SOAP访问Web服务

2013年12月11日 ⁄ 综合 ⁄ 共 3535字 ⁄ 字号 评论关闭

1.   SOAP

a)       
原理:SOAP可以理解为另外一种形式的XML-RPC实现,他也是在HTTP的协议体里面附带了所要调用的服务请求,或者服务结果。另外,SOAP可以定义一些额外的变量类型,而不仅是协议所规定的那部分。

另外,SOAP把他的消息封装到一个Envelop当中,Envelop包括了三部分,HeaderBodyAttachment。其中HeaderXML-RPC所没有的,他提供了一套额外的机制来保证程序的健壮性和安全性。例如mustUnderstand属性保证了服务方清楚了解请求方的要求。version保证不同版本的SOAP之间不会形成调用关系。actor属性,保证了消息能在既定的路线上传输。

 

2.   SOAP的请求和响应格式。

a)       
请求:

POST /soap/servlet/rpcrouter HTTP/1.0

Host: localhost:5555

Content-Type: text/xml; charset=utf-8

Content-Length: 440

SOAPAction: ""

 

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

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

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

<SOAP-ENV:Body>

<ns1:hello
xmlns:ns1="urn:HelloService"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<name
xsi:type="xsd:string">nick</name>

</ns1:hello>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

b)       
响应:

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

X-Powered-By: Servlet 2.4; JBoss-4.0.3RC1
(build: CVSTag=JBoss_4_0_3_RC1 date=200506260220)/Tomcat-5.5

Set-Cookie: JSESSIONID=0738F39D4811D19D2173AA02F7F081F6; Path=/

Content-Type: text/xml;charset=utf-8

Content-Length: 474

Date: Thu, 25 Aug 2005 01:30:56 GMT

Connection: close

 

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

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

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

<SOAP-ENV:Body>

<ns1:helloResponse
xmlns:ns1="urn:HelloService"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<return
xsi:type="xsd:string">Hello World nick</return>

</ns1:helloResponse>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

c)       
错误:

HTTP/1.1 500 Internal Server Error

Server: Apache-Coyote/1.1

X-Powered-By: Servlet 2.4; JBoss-4.0.3RC1
(build: CVSTag=JBoss_4_0_3_RC1 date=200506260220)/Tomcat-5.5

Set-Cookie: JSESSIONID=2B54BDA0260BA781A34936EB63A8B88B; Path=/

Content-Type: text/xml;charset=utf-8

Content-Length: 477

Date: Thu, 25 Aug 2005 01:33:59 GMT

Connection: close

 

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

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

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

<SOAP-ENV:Body>

<SOAP-ENV:Fault>

<faultcode>SOAP-ENV:Server</faultcode>

<faultstring>Method "hello2"
is not supported.</faultstring>

<faultactor>/soap/servlet/rpcrouter</faultactor>

</SOAP-ENV:Fault>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

 

3.   SOAP实例

a)       
发送请求:

public class Client {

     public
static void main(String[] args) throws Exception {

         URL
url = new URL("http://localhost:8080/soap/servlet/rpcrouter");

         Call
call = new Call();

         call.setTargetObjectURI("urn:HelloService");

         call.setMethodName("hello");

         call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

         Vector<Parameter>
pars = new Vector<Parameter>();

         pars.addElement(new
Parameter("name", String.class, "nick", null));

         call.setParams(pars);

         Response
res = call.invoke(url, "");

         if
(!res.generatedFault()) {

              Parameter
par = res.getReturnValue();

              System.out.println(par.getValue());

         }else{

              Fault
fault = res.getFault();

              System.out.println(fault);

         }

     }

}

 

b)       
发布服务:apache-soap是通过一个XML文件,来描述所要发布的服务的。

<?xml version="1.0"?>

<isd:service xmlns:isd=http://xml.apache.org/xml-soap/deployment

             id="urn:HelloService">

<isd:provider type="java" scope="Application" methods="hello">

<isd:java class="test.Service" static="false"/>

</isd:provider>

<isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>

</isd:service>

 

抱歉!评论已关闭.