Introduction
In this articleArticle, We will discuss how to use visual c ++ to develop a simple soap client applicationProgram, We will also introduce the use of soap APIs. Soap is a very popular protocol for information exchange on the Internet. It is very simple because it works in concert with HTTP, SMTP, and other similar protocols. The information described in it can be easily sent to another computer over the Internet, without the need to worry about being intercepted by network security technologies such as firewalls.
Here, we assume that the reader has a certain understanding of the SOAP protocol and is proficient in C ++. If you are not familiar with soap, you can view relevant information. We also assume that the reader is familiar with the use of COM, especially the smart pointer in COM, because in this article, we will use the import command to convert the COM interface to a smart pointer. In addition, you need to install Microsoft's soap toolkit.
Soap programming Basics
We will start our soap programming journey by introducing a class related to the basic soap application. However, we must first import the required type library before our applications can use the soap class.
Import Type Library
All objects and interfaces used in soap are included in mssoap1.dll, which is included in Microsoft soap Toolkit 2.0. We can find this file in c: \ Program Files \ common files \ mssoap \ binaries \ mssoap1.dll. Use the # import command to import the file to our source file. The content in the class library file will be converted to the COM smart pointer that describes the COM interface.
Soap uses XML as its data format, so we need Microsoft's XML parser to process XML content, which is included in msxml3.dll. Before importing the mssoap1.dll file, you must import the file as follows:
# 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 is all the class definitions required to develop a soap application. Three steps are required to develop a soap client application:
· Specify and connect to an Internet service.
· Prepare and send messages.
· Read the response from the server.
The following are the classes we need to use to develop a basic soap client application:
Soapconnector
In Client/Server mode, the first thing any client application needs to do is to connect to the server. Soapconnector is a protocol used to implement client and server application connector. It also serves as an abstract class that defines implementation of other protocol interfaces. That is to say, soap is not limited to serving as a specific protocol. We will find that some of its implementations also support MSMQ, MQ series, SMTP, and TCP/iptransports. For the sake of simplicity, I will only discuss its purpose as an HTTP transport, which is implemented by the httpconnector class in Microsoft soap Toolkit 2.0.
Steps required to use the soapconnector class
First, create an object of the soapconnector class:
Isoapconnectorptr connector; connector. createinstance (_ uuidof (httpconnector )); |
Then, specify the address of the web service. Next, we must describe the Web service in detail. A Web Service is specified by property (an attribute of httpconnector. When processing this attribute, you must specify the attribute we reference and the value of this attribute. Next, we use the endpointurl attribute to specify the Web Service:
Connector-> property ["endpointurl"] = "some URL pointing to Web Service "; |
The following table provides an attribute list (the attribute name is case sensitive)
Attribute |
Description |
Authpassword |
The password used for endpoint authentication. |
Authuser |
The username used for endpoint authentication. |
Endpointurl |
The endpoint URL. |
Proxypassword |
Proxy authentication password. |
Proxyport |
The port used by the proxy server. |
Proxyserver |
The host name or IP address of the proxy server. |
Proxyuser |
The username for proxy authentication. |
Soapaction |
The value of soapaction in the HTTP header. This attribute can only be set from a low-level API. If you use the connectorproperty attribute (Advanced API) in the soapclient interface to set this attribute, it will be ignored. |
Sslclientcertificatename |
If yes, the string indicates the client certificate used for the SSL protocol. Syntax: sslclientcertificatename [CURRENT_USER | LOCAL_MACHINE \ [store-name \] Certificate Name. The default name is CURRENT_USER \ My. |
Timeout |
The timeout time of httpconnector, which is measured in milliseconds. |
Useproxy |
A boolean attribute indicates whether a proxy server is used. By default, the value of this attribute is set to false, indicating that no proxy server is required. If you want to use the proxy server, you need to set the value of this attribute to true. If you set the value of this attribute to true without setting the proxyserver attribute, httpconnector uses the proxy server set in IE. Httpconnector ignores the "Do not use proxy server" setting in IE. |
Usessl |
Indicates whether an SSL Boolean value is used. If this attribute is set to true, the httpconnector object uses SSL connection regardless of whether HTTP or HTTPS is specified in the WSDL. If the value of this attribute is set to false, the httpconnector object uses SSL connection only when HTTPS is specified in the WSDL. |
Second, we need to connect to the web service. The Connect Method of the httpconnector class is used to initialize the soapconnector object and prepare to connect to the web service.
Connector-> connect ();
After connecting to the server, we need to specify the Operations completed by the web service. To specify this operation, we need to use the property property of soapconnector again:
Connector-> property ["soapaction"] = "some Uri "; |
After connecting to the Web Service and other details, we can call the method for sending soap information to the server. This method must be called before other methods of soapserializer are called:
Connector-> beginmessage (); |
After completing information-related operations, we must call the endmessage () function to truly send messages to Web Services.
. . [Message preparationCode] . . Connector-> endmessage (); |
The above steps are required to complete the actual connection with the web service. In the following section, we will discuss how to create and prepare an information.
Soapserializer
The soapserializer object is used to construct a SOAP message sent to the web service. Before connecting to the server, the soapserializer object must be connected to the soapconnector object. To connect these two objects to each other, we need to call the init method of the soapserializer object. This method requires an inputstream parameter (the stream that sends data to the server ):
// Create a soapserializer object and use inputstream to initialize it Isoapserializerptr serializer; serializer. createinstance (_ uuidof (soapserializer); serializer-> Init (_ variant_t (iunknown *) connector-> inputstream )); |
Before discussing other soapserializer functions, 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 main tag of the soap document. soap messages are typically encapsulated in the <envelope> element, the <envelope> element contains a specified message body marked by <body>. The message body contains the actual request. In C ++, there are very appropriate methods to create these tags and specify their values. The following code illustrates how to use these methods:
Serializer-> startenvelope ("Soap ","",""); // Start an element in the SOAP message. The first parameter describes the namespace, // If it is null, soap-env is used by default. Parameters 2 and 3 // Describes the URI and encoding types respectively. Serialsuppliers-> startbody (""); // Start of the <body> element in the message. The first parameter describes the Encoding URI, and its default value is none. Serializer-> startelement ("somemethodname", "M "); // Start of the child element of the <body> element in the SOAP message. The first parameter is the child element name. // The second parameter is Uri, the third parameter is the encoding type, and the last parameter is the element namespace. Serializer-> writestring ("someparametervalue ") // Write element value |
Functions starting with startxxx have functions starting with endxxx and ending element. After the message is completed, the system will call the connected endmessage () method to actually start sending messages to the service.
Now 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. Next we will discuss this issue.
Soapreader
This object reads the response from the web service and parses it into a dom for further processing. The following 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 obtain the result, we connect outputstream to read the response stored in the soapreader object (outputstream is used to receive data from web services ):
// Create soapreader object and code for connecting to outputstream Isoapreaderptr reader; reader. createinstance (_ uuidof (soapreader); reader-> load (_ variant_t (iunknown *) connector-> outputstream )); // The load method can also receive XML document files or strings |
After loading the Web service response to the soapreader object, we can call the rpcresult attribute of the soapreader object to obtain the corresponding result, but rpcresult does not return the real result, it returns the first child element of the first entry in the <body> element. You can call the text property to return the actual result:
Reader-> rpcresult-> text |
Example of a soap client application
To illustrate how to use the soap class discussed in this article, we use a service listed on http://www.xmethods.net/, which can display that the user is using Yahoo Messenger. It only requires one parameter, that is, the logon ID of the Yahoo user. The returned result is a Boolean value. 0 indicates that the user is offline, and 1 indicates that the user is online.
I always think that the best way to learn a programming technology is to study on the ground.Source codeHere, we will adopt this method. The following is the c ++ code used to call a console application that finds whether Yahoo users are 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; // Connect to the 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 )); // Create 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 the response Reader. createinstance (_ uuidof (soapreader )); // Connect reader to the output string of Connector Reader-> load (_ variant_t (iunknown *) connector-> outputstream ),""); // Display the result Printf ("Answer: % s \ n", (const char *) Reader-> rpcresult-> text ); Couninitialize (); } |
We can see that the code is very simple. Even if you have never used C ++, I ensure that the reader can understand the role of the code above: first, it is connected to a remote server; second, it creates a SOAP message and sends it to the web service. Finally, it reads the server response and outputs it to the screen using printf.
Conclusion
In this article, we discuss how to use visual c ++ to create a simple soap client application. We also learned several methods in the soap toolkit and how to use soap to obtain data from the server. I hope that through this article, you can learn how to use C ++ to develop soap client applications.