Creating a SOAP client application with visualc++ (i)

Source: Internet
Author: User
Tags soap soap client xml parser wsdl

Creating a SOAP client application with visualc++ (i)

The Soapserializer object is used to build a SOAP message that is sent to the Web service. The Soapserializer object must be connected to the Soapconnector object before connecting to the server. To make these two objects interconnected, we need to invoke the Init method of the Soapserializer object, which requires a parameter inputstream (the stream that sends the data to the server):

Create a Soapserializer object and initialize it with InputStream

Isoapserializerptr Serializer; Serializer.createinstance (_uuidof (Soapserializer)); Serializer->init (_variant_t ((iunknown*) connector->inputstream));

Before we discuss the other functions of Soapserializer, let's look at an example of a SOAP request:
<soap:envelope xmlns:soap= "SOAP namespace" >
<SOAP:Body>
<m:somemethodname xmlns:m= "some namespace" >
<someParameter> Someparametervalue </someParameter>
<m:someMethodName>
</SOAP:Body>
</SOAP:Envelope>

The SOAP request is encapsulated in the tag. The <Envelope> tag is the primary token of the SOAP document, and the SOAP message is usually encapsulated in the <Envelope> element, and the <Envelope> element contains a message body specified by the <Body> tag that contains the actual request. In C + +, there are very appropriate ways to create these tags and specify their values. The following code shows how to take advantage of these methods:

Serializer->startenvelope ("SOAP", "", "");
Begins an element in a SOAP message, the first parameter describes the namespace,

If it is a null value, Soap-env is used by default. Second, third parameter

The URI and the encoding type are described separately.
Serialzier->startbody ("");

The beginning of the <Body> element in the message, the first parameter describes the encoding style URI, and its default value is None.
Serializer->startelement ("Somemethodname", "", "" "," M ");

The beginning of the child element of the <Body> element in the SOAP message. The first parameter is a child element name

The second argument is the URI, the third argument is the encoding type, and the last parameter is the namespace of the element.
Serializer->writestring ("Someparametervalue")

Write the value of the element

Functions that begin with STARTXXX have corresponding functions that begin with endxxx and end the element. After the message is completed, the system calls the connected Endmessage () method to really start sending a message to the service.

Now that we are connected to the service, we have prepared our request and sent it to the service. The last step is to read the response from the server. Let's discuss the issue below.

Soapreader

The object reads the response from the Web service and resolves it to the DOM for further processing. Here is an example of a response from a Web service:
<soap:envelope xmlns:soap= "SOAP namespace" >
<SOAP:Body>
<m:somemethodnameresponse xmlns:m= "some namespace" >
<return> Someresult </return>
<m:someMethodNameResponse>
</SOAP:Body>
</SOAP:Envelope>

Before calling any method to get the result, we join OutputStream to read the response stored in the Soapreader object (OutputStream is used to receive data from the Web service):

Creating Soapreader objects and code that joins with OutputStream

Isoapreaderptr Reader; Reader.createinstance (_uuidof (Soapreader)); Reader->load (_variant_t ((iunknown*) connector->outputstream));

The Load method can also receive an XML document file or a string

After loading the response of the Web service into the Soapreader object, we can obtain the corresponding result by calling the Rpcresult property of the Soapreader object, but Rpcresult does not return the true result. It returns the first child element of the first entry in the <Body> element. We can return the true result by calling the Text property:
Reader->rpcresult->text

An example of a SOAP client application

To illustrate how to use the SOAP classes discussed in this article, we used a service listed on http://www.xmethods.net/that shows whether the user is using Yahoo Messenger. It requires only one parameter, the login ID of the Yahoo user. The returned result is a Boolean value, 0 indicates that the user is not in line, and 1 indicates the user is online.

I've always thought that the best way to learn some kind of programming technique is to learn the source code on the ground, and here we take that approach. Here is the C + + code for a console application that uses SOAP calls to discover whether a Yahoo user is online:

#include

#import "Msxml3.dll"
using namespace MSXML2;

#import "C:/Program Files/common files/mssoap/binaries/mssoap1.dll"/
Exclude ("IStream", "ISequentialStream", "_large_integer",/
"_ularge_integer", "Tagstatstg", "_filetime")

using namespace Mssoaplib;

void Main ()
{
CoInitialize (NULL);

Isoapserializerptr Serializer;
Isoapreaderptr Reader;
Isoapconnectorptr Connector;

Connecting to a Web service
Connector.createinstance (__uuidof (HttpConnector));
connector->property["Endpointurl"] = "http://www.allesta.net:51110/webservices/soapx4/isuseronline.php";
Connector->connect ();

Start message
connector->property["soapaction"] = "uri:allesta-yahoouserping";
Connector->beginmessage ();

Create a Soapserializer object
Serializer.createinstance (__uuidof (Soapserializer));

Connect the serializer to the connector input string
Serializer->init (_variant_t ((iunknown*) connector->inputstream));

Creating a SOAP message
Serializer->startenvelope ("", "", "");
Serializer->startbody ("");
Serializer->startelement ("Isuseronline", "uri:allesta-yahoouserping", "" "," M ");
Serializer->startelement ("username", "", "", "");
Serializer->writestring ("laghari78");
Serializer->endelement ();
Serializer->endelement ();
Serializer->endbody ();
Serializer->endenvelope ();

Send the message to the Web service
Connector->endmessage ();

Read response
Reader.createinstance (__uuidof (Soapreader));

The output string to join reader to the connector
Reader->load (_variant_t ((iunknown*) connector->outputstream), "");

Show results
printf ("Answer:%s/n", (const char *) reader->rpcresult->text);
CoUninitialize ();

}

As we can see, the code is very simple, and even if we haven't used C + +, I'm sure the reader will understand what the code does: first, it connects to the remote server, then it creates the SOAP message and sends the message to the Web service, and finally reads the server's response and prints it to the screen using printf.

Building a SOAP client application with visualc++ (ii)

First, Prerequisites:
You must be familiar with using COM, especially with smart pointers in COM. I converted the COM interface into a smart pointers by importing the method. The system must have Microsoft SOAP Toolkit and Microsoft XML Parser installed. The reference section at the end of this article describes how to download the Toolbox. At the end of this article, the source program can be downloaded.

Second, the basis of SOAP programming:
The following is a brief introduction to the classes contained in a simple SOAP application. Prior to this, the required type library must be imported before the program could use the SOAP class.

Import type library:
The objects and interfaces used in soap are in the Mssoap1.dll file. This file is generated when you install Microsoft SOAP Toolkit 2.0, and there is a path: "C:/Program Files/common files/mssoap/binaries/mssoap1.dll". Use #import to import the file into your program. The content of the type library is converted to COM smart pointers when it is imported to describe the COM interface. Because soap is completely dependent on XML, it is necessary to use Microsoft XML parser to process XML. Microsoft XML parser in the Msxml3.dll file. This file is to be imported before importing Mssoap1.dll.

#import "Msxml3.dll"

using namespace MSXML2;

#import "C:/Program Files/common files/mssoap/binaries/mssoap1.dll"/

Exclude ("IStream", "ISequentialStream", "_large_integer",/

"_ularge_integer", "Tagstatstg", "_filetime")

using namespace Mssoaplib;

The above code is required to write a SOAP program.

There are three steps to establishing a SOAP client application:
1-Specify and connect to the Web server.
2-Prepare and send messages.
Read the information returned by the service side.

Here is the class to use for the basic SOAP client:

1-soapconnector:
In customer/service mode, the first thing to do is to connect to the server. The Soapconnector class executes the message delivery protocol between the client and the server. Soapconnector is an abstract class that defines an interface for protocol execution. In fact, the Soapconnector class does not define a specific delivery protocol, such as MSMQ, MQ Series, SMTP, TCP/IP, and so on. For simplicity, this article only describes the use of the HTTP delivery protocol, which is performed by the HttpConnector class in Microsoft SOAP Toolkit 2.0.

The Soapconnector class uses the following steps:
A) Create the Soapconnector class object:
ISOAPCONNECTORPTR connector;
Connector.createinstance (__uuidof (HttpConnector));

b) Specify the Web server address:
Specify the server to do two things: Select the HttpConnector property and the corresponding property value. This example selects the Endpointurl property:
Connector->property ["endpointurl"] = "Some URL pointing to Web service";

The following is a description of the property options (property names are case sensitive):
Authpassword: Customer Password
Authuser: Customer Name
Endpointurl: Customer URL
Proxypassword: Proxy Password
ProxyPort: Agent Fracture
ProxyServer: IP address or host name of the proxy server
Proxyuser: Proxy User Name
The header value of the soapaction:http. This property is used only for low-level APIs. It ignores the Connectorproperty property in the SoapClient interface (Advanced API).
Sslclientcertificatename: Specifies that the secure Sockets Layer (SSL) encryption protocol is used. The syntax is as follows:
[Current_User | Local_machine/[store-name/]]cert-name with the defaults being current_user/my (same as Microsoft Internet Explorer usage).
The timeout limit for the timeout:httpconnector, in milliseconds.
UseProxy: Defines whether to use proxies. The default value is False. If this property is True (true) and the above ProxyServer value is not set, the proxy server will use the proxy server in IE. At this time HttpConnector will disregard IE's "Bypass Proxy" (bypass) settings.
UseSSL: Defines whether to use SSL (True or False). When this value is set to True, the HttpConnector object is SSL-connected regardless of whether the WSDL setting is HTTP or HTTPS. If this value is set to non-true, the HttpConnector object is only SSL-connected when the WSDL is set to HTTPS.

c) Connect to the Web server:
Connector->connect ();

d) Specify the action:
Connector->property ["soapaction"] = "some uri";

e) Start the message handle:
Must start the message processing mechanism before Soapserializer (message preparation function)
Connector->beginmessage ();

After the message has been processed, send the message to the server using the Endmessage () function.
.
.
[Message Preparation Code]
.
.
Connector->endmessage ();

The above is the process of connecting to the server. The following describes how to create and prepare messages.

Soapserializer:
Used to establish a SOAP message sent to the server. Before communicating with the server, the Soapserializer object must first be connected to the Soapconnector object. The initialization function of the Soapserializer will establish this internal connection. The parameter that initializes the surrogate is InputStream (data flow):
Creates a Soapserializer object and initializes it with InputStream.
Isoapserializerptr Serializer;
Serializer.createinstance (_uuidof (Soapserializer));
Serializer->init (_variant_t ((iunknown*) connector->inputstream));

Here is the SOAP request code:

<soap:envelope xmlns:soap= "SOAP namespace" >
<SOAP:Body>
<m:somemethodname xmlns:m= "some namespace" >
<someParameter> Someparametervalue </someParameter>
<m:someMethodName>
</SOAP:Body>
</SOAP:Envelope>

The SOAP request is placed in the tag. <Envelope> is the main token of the soap file. SOAP messages are usually placed in "envelopes" (Envelope). The message body is placed in the <Body> tag in the envelope, which contains the specific request. In C + +, the corresponding methods are used to interpret the tags and define the relevant values.
The following code shows how to use these methods:


Serializer->startenvelope ("SOAP", "", "");
Begins processing SOAP messages. The first parameter is a namespace, and the default is soap-env.
The second parameter defines a URI. The third parameter defines how the Serialzier->startbody ("") function is encoded.
Start processing the <Body> element, the first parameter is the encoding type of the URI, and the default is None.

Serializer->startelement ("Somemethodname", "", "" "," M ");
Begins processing child elements in the body.
The first parameter is the name of the element. The second argument is a URI.
The third parameter is the type of the encoding. The fourth parameter is the namespace of the element.

Serializer->writestring ("Someparametervalue")
Write element value

Each of the above startxxx functions is then terminated with the corresponding endxxx function. After the message is done, the connector calls the Endmessage () method to send the message to the server.

At this point, we have connected the server and made the corresponding message. The last step is to receive a server response.

Soapreader:
Reads the information returned by the server, parses the information and loads it into the DOM for further processing. The following is the SOAP response information returned by the server:

<soap:envelope xmlns:soap= "SOAP namespace" >
<SOAP:Body>
<m:somemethodnameresponse xmlns:m= "some namespace" >
<return> Someresult </return>
<m:someMethodNameResponse>
</SOAP:Body>
</SOAP:Envelope>

Use OutputStream to read the information in the Soapreader object. (OutputStream the information returned by the receiving server).

Create a Soapreader object and connect to the OutputStream
Isoapreaderptr Reader;
Reader.createinstance (_uuidof (Soapreader));
Reader->load (_variant_t ((iunknown*) connector->outputstream));
The Load method can also be used to load an XML file or string

After you load the response information into the Soapreader object, you can use its Rpcresult property to get the result. However, but Rpcresult does not directly return the result, it returns the first entity element of <Body>, and then reads the element property value with the Text property:
Reader->rpcresult->text

Third, illustrate a simple SOAP client application:
This example uses Www.xmethods.net as a server. This server points to Yahoo online information.
Details can be found in the http://www.xmethods.net/ve2/ViewListing.po?serviceid=156.
In the following code, you enter a parameter, which is the user ID of Yahoo. The return result is 0 for offline and 1 for online.
Additional details can be found at: http://www.allesta.net:51110/webservices/wsdl/YahooUserPingService.xml

Iv. References:
The SOAP specification simple Object Access Protocol (soap) 1.1-W3C Note:
Http://www.w3.org/TR/SOAP
Microsoft SOAP Toolkit Download:
Http://download.microsoft.com/download/xml/soap/2.0/w98nt42kme/EN-US/SoapToolkit20.exe

Five: SOAP code for the sample in this article:
#include <stdio.h>

#import "Msxml3.dll"
using namespace MSXML2;

#import "C:/Program Files/common files/mssoap/binaries/mssoap1.dll"/
Exclude ("IStream", "ISequentialStream", "_large_integer",/
"_ularge_integer", "Tagstatstg", "_filetime")
using namespace Mssoaplib;

void Main ()
{
CoInitialize (NULL);

Isoapserializerptr Serializer;
Isoapreaderptr Reader;
Isoapconnectorptr Connector;

Connecting to a server
Connector.createinstance (__uuidof (HttpConnector));
connector->property["Endpointurl"] = "http://www.allesta.net:51110/webservices/soapx4/isuseronline.php";
Connector->connect ();

Start message mechanism
connector->property["soapaction"] = "uri:allesta-yahoouserping";
Connector->beginmessage ();

Create a Soapserializer object
Serializer.createinstance (__uuidof (Soapserializer));

Connecting to an input stream
Serializer->init (_variant_t ((iunknown*) connector->inputstream));

Making SOAP Information
Serializer->startenvelope ("", "", "");
Serializer->startbody ("");
Serializer->startelement ("Isuseronline", "uri:allesta-yahoouserping", "" "," M ");
Serializer->startelement ("username", "", "", "");
Serializer->writestring ("laghari78");
Serializer->endelement ();
Serializer->endelement ();
Serializer->endbody ();
Serializer->endenvelope ();

Sending information to the server
Connector->endmessage ();

Read response
Reader.createinstance (__uuidof (Soapreader));

Connecting the output stream
Reader->load (_variant_t ((iunknown*) connector->outputstream), "");

Show results
printf ("Answer:%s/n", (const char *) reader->rpcresult->text);
CoUninitialize ();
}

Creating a SOAP client application with visualc++ (i)

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.