如何使用Soap消息调用Web Services
2007-09-18 09:12
716 查看
一 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 (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!"; } } |
发布后我们可以在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> |
<?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> |
|
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消息
<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
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>. |
x.LoadXml responseXml String s= x.getElementsByTagName("return").Item(0).Text Text1.text=s |
5.整个事件的处理过程如下:
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 Services
- 使用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 如何使用SOAP:Lite Perl程序库调用基于.Net框架的Web服务
- 用Soap消息调用Web Services(续)
- 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息
- 用Soap消息调用Web Services
- 用Soap消息调用Web Services(续)
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- 用Soap消息调用Web Services
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- .Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理
- 如何使用空格键调用Windows7启动管理器
- 【翻译】MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API
- PHP使用SOAP调用.net的WebService问题
- Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
- 如何使用jquery GET方式请求调用asp.net方法
- 使用zeng studio 创建的php调用webservice soap简单实例