Use Soap messages to call Web Services

Source: Internet
Author: User
Tags abstract definition format definition xml parser

Http://www.dlmu.net/pe/computer/ShowArticle.asp? ArticleID = 6375

Web Services enables data sharing and communication between heterogeneous computing environments to achieve information consistency. With the increasing environment of heterogeneous computers, we will call Web Services written and released in different computer systems more frequently, in this article, the author provides a practical process of using VB Applications to call the java platform to write and publish Web Services, and analyzes how to construct SOAP messages based on the WSDL file.

I. Introduction to SOAP

1. SOAP Definition

Simple Object Access Protocol (SOAP) is a Simple Protocol for exchanging information in a distributed or distributed environment. It is an XML-based Protocol.

It consists of four parts:

SOAP encapsulation (envelop): a framework that defines the content in a message, who sends the message, who should accept and process it, and how to process it;
SOAP encoding rules (encoding rules): used to represent the instance of the Data Type required by the application;
RPC representation: indicates the protocol for remote process calls and responses;
SOAP binding: Exchange information using the underlying protocol.

2. SOAP messages

SOAP uses two widely used protocols: HTTP and XML. HTTP is used to implement RPC-style transmission of SOAP, while XML is its encoding mode. a soap request is actually an http post request.

For more detailed SOAP information, see the http://www.w3.org/TR/2001/WD-soap12-20010709/

II. Introduction to WSDL

Web Services Description Language (WSDL) is an XML Application that describes a Web service as a set of service access points.

The WSDL document describes a Web service as a set of network endpoints or ports. In the WSDL, the abstract definitions of service access points and messages have been separated from the specific service period deployment and Data Format binding, therefore, you can use these abstract objects again: messages are abstract descriptions of the information to be exchanged; port types are abstract sets of operations provided to Web Services.

A specific protocol and data format definition for a specific port type constitute a usable binding. A port is defined as a reusable binding and network address association, A group of ports constitute a service.

WSDL uses the following elements when defining Web Sevices:

Types: a data-type container that uses some type systems (such as common XSD)
Message: an abstract definition of communication messages.
Operation: abstract description of the operations provided by the Service
Port Type: Abstraction of a group of operations supported by one or more endpoints
Binding: Protocol and Data Format definitions for specific port types
Port: defines a single endpoint associated with the binding and network address.
Service: A combination of related endpoints

For detailed WSDL definitions and related information, see: http://www.w3c.org/TR/wsdl

3. Use WSAD to develop and publish a Web Service

1. We use WSAD (Websphere Studio Application Developer) to create a HelloWorld java class, which has only one method, that is, to return a string HelloWorld.

// HelloWorld. java

Package hello;

Public class HelloWorld {
Public String getString (){
Return "Hello World! ";
}
}

2. Publish this type as a web service to the server in the WASD test environment.

After the release, we can find two in the web project's wsdl file. the wsdl file: HelloWorld-service.wsdl, HelloWorld-binding.wsdl, HelloWorld-binding.wsdl file mainly describes the service access point of this web services, the HelloWorld-binding.wsdl file describes the data structure of the web services communication message, the operations supported by each access point, the specific Protocol of the specific fracture type and the binding of the data format specification, etc, for more information, see deploy Web services using WSDL:

Part 1 (http://www-900.cn.ibm.com/developerworks/cn/webservices/ws-intwsdl/part1/index.shtml) Article HelloWorld-service.wsdl content is as follows:

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

The HelloWorld-binding.wsdl content is as follows:

<? 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. Start the test server in WSAD

4. Use VB to write client programs

1. Prepare the environment

You need to use SOAP messages to access Web Service messages. You must first download the Soap ToolKit (http://download.microsoft.com/download/xml/soap/2.0/W98NT42KMe/EN-US/SoapToolkit20.exe) provided by MicroSoft)

2. Compile a VB Application

Open VB and create a new project

The text section is used to display the returned information.

3. Compile a SOAP message

<S: Envelope xmlns: s = 'HTTP: // schemas.xmlsoap.org/soap/envelope /'
Xmlns: xsi = 'HTTP: // www.w3.org/5o/xmlschema-instance'
Xmlns: xsd = 'HTTP: // www.w3.org/5o/xmlschema'>
<S: Body>
<M: getString xmlns: m = 'urn: helloworld'>
</M: getString>
</S: Body>
</S: Envelope>

Note:

The "getString" used here is the operation defined in defines-> portType-> operation in the HelloWorld-binding.wsdl, and urn: helloWorld is the namespace defined by defines-> portType-> operation-> soap: body in the HelloWorld-binding.wsdl

4. Display processing results

The returned result is also an XML message, as shown below:

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

We need to use the xml parser in ToolKit to parse it to get the results we need:

X. LoadXml responseXml

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

Text1.text = s

In this way, the web service call and processing process are completed. The operation results are shown as follows:

5. The entire event processing process is as follows:

Private Sub commandementclick ()
'Define an http object and send a post message to the server.
Dim h As MSXML2.ServerXMLHTTP40
'Define an XML Document Object and convert the handwritten or accepted XML content into an XML Object
Dim x as MSXML2.DOMDocument40

'Initialize the XML Object
Set x = New MSXML2.DOMDocument40

'Convert a handwritten SOAP string to an XML Object
X. LoadXml "<s: Envelope
Xmlns: s = 'HTTP: // schemas.xmlsoap.org/soap/envelope /'
Xmlns: xsi = 'HTTP: // www.w3.org/5o/xmlschema-instance'
Xmlns: xsd = 'HTTP: // www.w3.org/5o/xmlschema'>
<S: Body>
<M: getString xmlns: m = 'urn: helloquery'>
</M: getString>
</S: Body>
</S: Envelope>"

'Initialize the http object
Set h = New MSXML2.ServerXMLHTTP40

'Send Post message to the specified URL
H. open "POST", "http: // localhost: 8080/WForecast/servlet/rpcrouter"
H. send (x)

While h. readyState <> 4
Wend

'Display returned XML Information
Text1.Text = h. responseText

'Parse the returned XML Information and display the returned value.
X. LoadXml Text1.Text
Text1.Text = x. getElementsByTagName ("return"). Item (0). Text

End Sub

Conclusion 5

This article provides an example of using vbprogram to call Web Services written on the java platform. It also analyzes in detail how to call Web Services for communication and information sharing between heterogeneous computing platforms using SOAP. through the above practices, we have a comprehensive understanding of the Web Services calling mechanism using SOAP. the procedure and method for using other languages to implement SOAP to call Web Services are similar to those described above. Generally, you can refer to the above procedure for implementation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.