Use Visual C ++ to create a soap client application-foxer.net
Font size: medium and large | print published: Author: Libia Source: View: 15 times
Soap is a widely used protocol in information exchange. It is easy to use and can work directly with HTTP, SMTP, and other protocols. This article discusses how to use Microsoft soap Tookit C ++ to create a simple soap client application.
========================================================== ========================
I. Prerequisites:
You must be familiar with the use of COM, especially the smart pointers in COM. I use the import method to convert the COM interface to smart pointers. Microsoft soap toolkit and Microsoft XML Parser must be installed. The following section describes how to download the toolkit. The source code of this article can be downloaded from the attachment at the end of this article.
Ii. Soap programming basics:
The following describes the classes contained in a simple soap application. Before that, you must first import the required type library before the program can use the soap class.
Import Type Library:
All objects and interfaces used in soap are in the mssoap1.dll file. This file is generated when Microsoft soap Toolkit 2.0 is installed. The path is "C:/program files/common files/mssoap/binaries/mssoap1.dll ". Use # import to import the file to the program. The content of the Type Library is converted to COM smart pointers during import to describe the COM interface. Because soap fully depends on XML, Microsoft XML Parser must be used to process XML. Microsoft XML parser is in the msxml3.dll file. This file must be imported before mssoap1.dll is imported.
# 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 for writing a soap program.
To create a soap client application, follow these three steps:
1-specify and connect to the web server.
2-prepare and send messages.
3-read the information returned by the server.
The following are the classes to be used on the basic soap client:
1-soapconnector:
In the customer/service mode, the first thing to do is to connect to the server. The soapconneclass executes the message transfer protocol between the client and the server. Soapconnector is an abstract class that defines the interface for protocol execution. In fact, the soapconnector class does not define a specific transfer protocol, such as MSMQ, MQ series, SMTP, and TCP/IP. For simplicity, this article only describes how to use the HTTP transfer protocol, which is executed by the httpconnector class in Microsoft soap Toolkit 2.0.
To use the soapconnector class, follow these steps:
A) create a soapconnector Class Object:
Isoapconnectorptr connector;
Connector. createinstance (_ uuidof (httpconnector ));
B) Specify the Web server address:
Specify the server. To do this, select the attributes and corresponding attribute values of httpconnector. In this example, select the endpointurl attribute:
Connector-> property ["endpointurl"] = "some URL pointing to Web Service ";
The following describes the attribute options (the attribute name is case sensitive ):
Authpassword: Customer Password
Authuser: customer name
Endpointurl: Customer URL
Proxypassword: proxy Password
Proxyport: proxy fracture
Proxyserver: IP address or Host Name of the proxy server
Proxyuser: proxy User Name
Soapaction: HTTP header value. This attribute is only used for low-level APIs. It ignores the ororproperty attribute in the soapclient interface (Advanced API.
Sslclientcertificatename: Specifies the Secure Sockets Layer (SSL) encryption protocol. Syntax:
[CURRENT_USER | LOCAL_MACHINE/[store-name/] cert-name with the defaults being CURRENT_USER/My (same as Microsoft Internet Explorer ).
Timeout: timeout limit of httpconnector, in milliseconds.
Useproxy: defines whether to use proxy ). The default value is false. If this attribute is true and the preceding proxyserver value is not set, the proxy server uses the proxy server in IE. Httpconnector ignores the "Bypass proxy" (bypass) settings of IE.
Usessl: defines whether to use SSL (true or false ). When this value is set to true, the httpconnector object uses SSL connection regardless of whether the WSDL is set to HTTP or HTTPS. If this value is not true, the httpconnector object is connected only in SSL mode when the WSDL is set to HTTPS.
C) connecting to the Web server:
Connector-> connect ();
D) specified action:
Connector-> property ["soapaction"] = "some Uri ";
E) Start the message handle:
The message processing mechanism must be enabled before soapserializer (message preparation function ).
Connector-> beginmessage ();
After the message is processed, use the endmessage () function to send the message to the server.
.
.
[Message preparation Code]
.
.
Connector-> endmessage ();
The above is the process of connecting to the server. The following describes how to create and prepare a message.
Soapserializer:
Creates a SOAP message sent to the server. Before communicating with the server, the soapserializer object must be connected to the soapconnector object. The initialization function of soapserializer establishes this internal connection. The initialization parameter is inputstream (Data Stream ):
// Create a soapserializer object and initialize it with inputstream.
Isoapserializerptr serializer;
Serializer. createinstance (_ uuidof (soapserializer ));
Serializer-> Init (_ variant_t (iunknown *) connector-> inputstream ));
The following is the SOAP request code:
Someparametervalue
The soap request is placed in the tag. Is the main identifier of a soap file. Soap information is usually stored in an "envelope" (envelope. The tag in the envelope contains a specific request. In C ++, the corresponding methods are used to interpret these tags and define related values.
The following code describes how to use these methods:
Serializer-> startenvelope ("Soap ","","");
// Start processing soap messages. The first parameter is the namespace. The default parameter is soap-Env.
// The second parameter defines the URI. The third parameter defines the encoding method of the serialsuppliers-> startbody ("") function.
// Start processing elements. The first parameter is the URI encoding type. The default value is none.
Serializer-> startelement ("somemethodname", "M ");
// Start processing the child elements in the body.
// The first parameter is the element name. The second parameter is Uri.
// Encoding type of the third parameter. The fourth parameter is the namespace of the element.
Serializer-> writestring ("someparametervalue ")
// Write element value
Each startxxx function must end with an endxxx function. After the message is complete, the connector calls the endmessage () method to send the message to the server.
So far, we have connected to the server and made the corresponding message. The last step is to receive the server response.
Soapreader:
Read the information returned by the server, parse the information and load it into the dom for further processing. Below are the soap response information returned by the server:
Someresult
Use outputstream to read information in the soapreader object. (Outputstream receives the information returned by the server ).
// Create a soapreader object and connect it to outputstream
Isoapreaderptr reader;
Reader. createinstance (_ uuidof (soapreader ));
Reader-> load (_ variant_t (iunknown *) connector-> outputstream ));
// The load method can also be used to load XML files or strings
After loading the response information to the soapreader object, you can use its rpcresult attribute to obtain the result. However, but rpcresult does not directly return the result. It returns the first object element and then reads the attribute value of this element using the text attribute:
Reader-> rpcresult-> text
3. An example of a simple soap client application:
In this example, www.xmethods.net is used as the server. This server points to Yahoo online information.
Can be in http://www.xmethods.net/ve2/ViewListing.po? Serviceid = 156 find the relevant details.
In the following code, enter a parameter, that is, the Yahoo user ID. 0 indicates offline, and 1 indicates online.
For more details, see http://www.allesta.net: 51110/WebServices/WSDL/yahoouserpingservice. xml
Iv. Reference:
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
5. the soap code in this example:
# 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;
// Connect to the server
Connector. createinstance (_ uuidof (httpconnector ));
Connector-> property ["endpointurl"] = "http://www.allesta.net: 51110/WebServices/soapx4/isuseronline. php ";
Connector-> connect ();
// Start the message mechanism
Connector-> property ["soapaction"] = "URI: allesta-yahoouserping ";
Connector-> beginmessage ();
// Create a soapserializer object
Serializer. createinstance (_ uuidof (soapserializer ));
// Connect to the input stream
Serializer-> Init (_ variant_t (iunknown *) connector-> inputstream ));
// Create 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 ();
// Send information to the server
Connector-> endmessage ();
// Read response
Reader. createinstance (_ uuidof (soapreader ));
// Connect the output stream
Reader-> load (_ variant_t (iunknown *) connector-> outputstream ),"");
// Display the result
Printf ("Answer: % s/n", (const char *) Reader-> rpcresult-> text );
Couninitialize ();
}
Attachment
Http://www.topxml.com/snippetcentral/snippetfiles/v20020425121357.zip