SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议.
它包括四个部分:
- SOAP封装(envelop): 封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;
- SOAP编码规则(encoding rules): 用于表示应用程序需要使用的数据类型的实例;
- SOAP RPC表示(RPC representation): 表示远程过程调用和应答的协定;
- SOAP绑定(binding): 使用底层协议交换信息。
SOAP采用了已经广泛使用的两个协议:HTTP和XML。其中HTTP用于实现SOAP的RPC风格的传输,而XML是它的编码模式,一个SOAP请求实际上就是一个HTTP POST请求。
其它详细的SOAP方面的信息请参考 http://www.w3.org/TR/2001/WD-soap12-20010709/
|
WSDL (Web Services Description Language)是一种XML Application,他的作用是将一个Web Services描述为一组服务访问点.
WSDL文档将一个Web Services描述成一组网络端点或者端口,在WSDL中,由于服务访问点和消息的抽象定义已经和具体的服务期部署和数据格式绑定分离,因此可以再次使用这些抽象对象: 消息,是对需要交换信息的抽象描述;端口类型,是对Web Service提供的操作的抽象集合。
特定端口类型的具体协议和数据格式定义构成了一个可以从用的绑定,一个端口定义成一个可重用绑定和网络地址的关联,一组端口构成了一个服务。
WSDL在定义Web Sevices时使用了以下元素:
- Types: 数据类型的容器,他采用一些类型系统(比如常用的XSD)
- Message: 通信消息的抽象类型化定义
- Operation: 服务提供的操作的抽象化描述
- Port Type: 一个或者多个端点支持的一组操作的抽象
- Binding: 特定端口类型的具体协议和数据格式定义
- Port:定义为binding和网络地址的关联的单个的端点
- Service: 一组相关的端点的结合
详细的WSDL定义和相关信息请参考: http://www.w3c.org/TR/wsdl
|
1.我们用WSAD(Websphere Studio Application Developer)创建一个HelloWorld的java类,他只有一个方法,就是返回一个字符串HelloWorld。
// HelloWorld.java
package hello;
public class HelloWorld {
public String getString(){
return "Hello World!";
}
}
|
2.将该类作为web service 发布到WASD带的测试环境的服务器上
发布后我们可以在web工程的wsdl文件下面找到两个.wsdl文件: HelloWorld-service.wsdl, HelloWorld-binding.wsdl,HelloWorld-binding.wsdl文件中主要描述了这个web services的服务访问点,HelloWorld-binding.wsdl文件中则描述了这个web services的通信消息的数据结构、每个访问点支持的操作、特定的断口类型的具体协议和数据格式规范的绑定等信息,其具体的含义可以参考使用 WSDL 部署 Web 服务:
第 1 部分(http://www-900.cn.ibm.com/developerworks/cn/webservices/ws-intwsdl/part1/index.shtml)一文 HelloWorld-service.wsdl的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloWorldService" targetNamespace="http://localhost:808/WForecast/wsdl/HelloWorld-service.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://localhost:808/WForecast/wsdl/HelloWorld-service.wsdl"
xmlns:binding="http://www.helloworld.com/definitions/HelloWorldRemoteInterface"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<import namespace="http://www.helloworld.com/definitions/HelloWorldRemoteInterface"
location="http://localhost:808/WForecast/wsdl/HelloWorld-binding.wsdl"/>
<service name="HelloWorldService">
<port name="HelloWorldPort" binding="binding:HelloWorldBinding">
<soap:address location="http://localhost:808/WForecast/servlet/rpcrouter"/>
</port>
</service>
</definitions>
|
HelloWorld-binding.wsdl的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloWorldRemoteInterface"
targetNamespace="http://www.helloworld.com/definitions/HelloWorldRemoteInterface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://www.helloworld.com/definitions/HelloWorldRemoteInterface"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<message name="getString">
<part name="result" type="xsd:string"/>
</message>
<portType name="HelloWorldJavaPortType">
<operation name="getString">
<output name="getString" message="tns:getString"/>
</operation>
</portType>
<binding name="HelloWorldBinding" type="tns:HelloWorldJavaPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getString">
<soap:operation soapAction="" style="rpc"/>
<output name="getString">
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:HelloWorld"/>
</output>
</operation>
</binding>
</definitions>
|
3. 启动WSAD中的测试服务器
|
要使用SOAP消息访问Web Service消息。必须先下载MicroSoft公司提供的Soap ToolKit( http://download.microsoft.com/download/xml/soap/2.0/W98NT42KMe/EN-US/SoapToolkit20.exe)
打开VB,创建一个新的工程
其中的文本款用于显示返回的信息。
<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd='http://www.w3.org/1999/XMLSchema'> <s:Body> <m:getString xmlns:m='urn:HelloWorld'> </m: getString > </s:Body> </s:Envelope> |
说明:
<m:getString xmlns:m='urn:HelloWorld'>其中用到的" getString"就是HelloWorld-binding.wsdl中定义的defines-> portType->operation中定义的操作,而urn:HelloWorld则是HelloWorld-binding.wsdl中defines-> portType->operation->soap:body定义的namespace
返回的结果也是一个XML消息,如下:
<?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:getStringResponse xmlns:ns1="urn:HelloWorld " SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="xsd:string">Hello World!</return> </ns1:getStringResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>. |
我们需要使用ToolKit中的xml解析器对他进行解析才能获取我们需要的结果:
x.LoadXml responseXml
String s= x.getElementsByTagName("return").Item(0).Text
Text1.text=s
|
这样我们对web service的调用和处理过程就结束了,操作的结果图示如下:
Private Sub Command1_Click()
'定义一个http对象,一边向服务器发送post消息
Dim h As MSXML2.ServerXMLHTTP40
'定义一个XML的文档对象,将手写的或者接受的XML内容转换成XML对象
Dim x as MSXML2.DOMDocument40
'初始化XML对象
Set x = New MSXML2.DOMDocument40
'将手写的SOAP字符串转换为XML对象
x.LoadXml "<s:Envelope
xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/1999/XMLSchema'>
<s:Body>
<m:getString xmlns:m='urn:HelloQuery'>
</m:getString>
</s:Body>
</s:Envelope>"
'初始化http对象
Set h = New MSXML2.ServerXMLHTTP40
'向指定的URL发送Post消息
h.open "POST", "http://localhost:8080/WForecast/servlet/rpcrouter"
h.send (x)
While h.readyState <> 4
Wend
'显示返回的XML信息
Text1.Text = h.responseText
'将返回的XML信息解析并且显示返回值
x.LoadXml Text1.Text
Text1.Text = x.getElementsByTagName("return").Item(0).Text
End Sub
|
|
本文给出一个VB程序调用java平台编写的Web Services的实例,并且结合实例详细的分析了使用SOAP如何在异种计算平台之间调用Web Services通信和共享信息的过程.通过上面的实践,我们对使用SOAP调用Web Services的机制有了全面的了解.使用其它语言来实现SOAP调用Web Services的过程和方法和上面的过程大同小异,一般可以参照上面的过程来实现.
本文介绍使用SOAP协议实现不同平台间的Web服务调用过程。通过VB客户端调用Java平台发布的Web服务,详细展示了SOAP消息的构造、发送及响应解析。
1287

被折叠的 条评论
为什么被折叠?



