科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道使用Soap消息调用Web Services

使用Soap消息调用Web Services

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Web Services技术使异种计算环境之间可以共享数据和通信,达到信息的一致性。随着异种计算机环境的不断增加,我们会更加经常的调用各种不同计算机体系中编写和发布的Web Services。

作者:中国IT实验室 来源:中国IT实验室 2007年9月22日

关键字: SOAP Web Services 编程 java

  • 评论
  • 分享微博
  • 分享邮件

    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的内容如下:

<?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中的测试服务器

    四 用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消息

<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>

    说明:

其中用到的" 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>.

    我们需要使用ToolKit中的xml解析器对他进行解析才能获取我们需要的结果:

x.LoadXml responseXml

String s= x.getElementsByTagName("return").Item(0).Text

Text1.text=s

    这样我们对web service的调用和处理过程就结束了,操作的结果图示如下:

    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的过程和方法和上面的过程大同小异,一般可以参照上面的过程来实现.

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件