web services技术使异种计算环境之间可以共享数据和通信,达到信息的一致性。随着异种计算机环境的不断增加,我们会更加经常的调用各种不同计算机体系中编写和发布的web services,作者在本文中给出了使用vb应用程序调用java平台编写和发布的web services的实践过程, 并且分析了如何根据wsdl文件构造soap消息的方法。
一 soap简介
1.soap定义
soap(simple object access protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于xml的协议.
它包括四个部分:
soap封装(envelop): 封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;
soap编码规则(encoding rules): 用于表示应用程序需要使用的数据类型的实例;
soap rpc表示(rpc representation): 表示远程过程调用和应答的协定;
soap绑定(binding): 使用底层协议交换信息。
2.soap消息
soap采用了已经广泛使用的两个协议:http和xml。其中http用于实现soap的rpc风格的传输,而xml是它的编码模式,一个soap请求实际上就是一个http post请求。

其它详细的soap方面的信息请参考 http://www.w3.org/tr/2001/wd-soap12-20010709/
二 wsdl简介
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
三 使用wsad开发和发布一个web services
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的内容如下:
<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中的测试服务器
四 用vb编写客户端程序
1.环境准备
要使用soap消息访问web service消息。必须先下载microsoft公司提供的soap toolkit( http://download.microsoft.com/download/xml/soap/2.0/w98nt42kme/en-us/soaptoolkit20.exe)
2.编写vb应用
打开vb,创建一个新的工程

其中的文本款用于显示返回的信息。
3.编写soap消息
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>
说明:
4.显示处理结果
返回的结果也是一个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的调用和处理过程就结束了,操作的结果图示如下:

5.整个事件的处理过程如下:
'定义一个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的过程和方法和上面的过程大同小异,一般可以参照上面的过程来实现.
闽公网安备 35060202000074号