SOAP is a very wide-wide agreement in information exchange, easy to use, and can work together with other protocols such as HTTP, SMTP. This article discusses how to use Microsoft SOAP Tookit C to create a simple SOAP client application. ============================================================================================================================================================================================================= ======= First, prerequisites: You must be familiar with the use of COM, especially to familiarize the Smart Pointers in COM. I convert the COM interface into Smart Pointers by importing methods. Microsoft Soap Toolkit and Microsoft XML Parser must be installed. Refer to a section of the article describes how to download the toolbox. The article is available in the article.
Second, SOAP Programming Basics: The following starts to introduce the classes contained in a simple SOAP application. Prior to this, you must first import the desired type library, then the program can use the SOAP class.
Import Type Library: Objects and interfaces used in SOAP are in the mssoap1.dll file. This file is generated when installing Microsoft SOAP Toolkit 2.0, with a path: "C: / Program files / compon files / mssoap / binaries / mssoap1.dll". Import this file into the program with #import. The content of the type library is converted to COM Smart Pointers when importing. Since 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 import before importing MSSOAP1.DLL.
#import "msxml3.dll"
USING NAMESPACE MSXML2;
#import "c: / program files / compon files / mssoap / binaries / mssoap1.dll" /
Exclude ("iStream", "ISEquentialStream", "_large_integer", / /
"_Ularge_integer", "tagstatstg", "_filetime")
USING NAMESPACE MSSOAPLIB;
The above code is to write SOAP programs must be included.
Establishing SOAP client applications with the following three steps: 1 - Specify and connect to the web server. 2 - Prepare and send a message. 3- Read the information returned by the server.
Below is the class to use at the basic SOAP client:
1- SOAPCONNECTOR: In Customer / Service mode, the first thing to do is to connect the server. The SOAPConnector class performs the message transfer protocol between the client and the server. The SOAPConnector is an abstract class that defines the interface executed by the protocol. In fact, the SOAPConnector class does not define a specific transfer protocol, such as: MSMQ, MQ Series, SMTP, and TCP / IP. For the sake of simplicity, this article only describes the use of the HTTP transfer protocol, which is performed by the HTTPConnector class in Microsoft SOAP Toolkit 2.0.
The SOAPCONNECTOR class use steps are as follows: a) Create a SOAPConnector class object: IsoapConnectorPtr Connector; Connector.createInstance (__ uuidof (httpconnector);
b) Specify the web server address: Specify the server, do two things: Select the attribute of the HTTPConnector and the corresponding attribute value. This example uses endpointurl attributes: connector-> proty ["endpointURL"] = "some url pointing to web service";
The following is the property option description (the property name is case sensitive): Authpassword: Customer password Authuser: Customer Name EndPointURL: Customer URL ProxyPassword: Agent Password Proxyport: Agent Drake Proxyserver: The IP address of the proxy server or hostname ProxyUser: Agent User Name SOAPACTION: HTTP's header value. This attribute is only used in the low-level API. It will ignore the CONNECTORPROPERTY attribute in the SOAPCLIENT interface (Advanced API). SSLClientCertificateName: Specifies the use of Secure Sockets Layer Encryption Protocol. 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). Timeout: The timeout limit of HTTPConnector, in milliseconds. UseProxy: Defines whether you use a proxy (Proxy). The default is false. If this property is true (TRUE), the proxy server will use the proxy server in IE. At this point, HTTPConnector will ignite the "Bypass Proxy" setting of IE. USESSL: Defines whether to use SSL (TRUE or FALSE). This value is set to true, and the HTTPConnector object uses an SSL connection mode regardless of the WSDL settings is HTTP or HTTPS. If this value is set to be non-true, the HTTPConnector object is only connected to the SSL mode only when WSDL is set to HTTPS. C) Connection with the web server: connector-> connect ();
d) Specify action: connection -> Property ["soapaction"] = "some uri";
e) Start the message handle: You must start the message processing mechanism before the SOAPSERIALIZER (Message Preparation Function);
After the message processing is completed, send the message to the server with the endMessage () function. [Message preparation code] .. connector-> endmessage ();
The above is the process of connecting to the server. Here is how to create and prepare messages.
SOAPSERIALIZER: It is used to establish a SOAP message sent to the server. The SOAPSERIALIZER object must be connected to the SOAPConnector object before communicating with the server. The starting function of SOAPSerializer will establish this internal connection. The initialized parameters are inputStream: // Create a SOAPSERIALIZER object and initialize with InputStream. IsoapserializerPtr Serializer; Serializer.createInstance (_UUIDOF (SOAPSERIALIZER)); serializer-> init (_variant_t ((iUnknown *) connectionor-> inputstream)); below is SOAP request code:
SOAP requests are placed in tags.
Serializer-> StarTenvelope ("SOAP", "," "); // Start processing SOAP messages. The first parameter is named space, default is SOAP-ENV. // Second parameter defines the URI. The third parameter defines the encoding method of the Serialzier-> StartBody (") function. // Start processing
element, the first parameter is the encoding type of the URI, default is NONE.Serializer-> StartElement ("SomeMethodName", "," "," M "); // Start processing child elements in the body. // The first parameter is the element name. The second parameter is the URI. // The third parameter encoding type. The fourth parameter is the naming space of the element.
Serializer-> Writestring ("SomeParameterValue" // Write Element Value
Although the corresponding ENDXXX function must be ended after each STARTXXX function above. After the message is finished, the connector calls the endMessage () method to send the message to the server.
At this point, we have connected the server to make the corresponding message. The last step is that the receiving server responds.
SOAPReader: Read the information returned by the server and loads the information into the DOM to further handle it. Below is the SOAP response information returned by the server:
// Create SOAPReader objects, and connected to outputstream ISoapReaderPtr Reader; Reader.CreateInstance (_uuidof (SoapReader)); Reader-> Load (_variant_t ((IUnknown *) Connector-> OutputStream)); // load method may also be used to load XML file or string
After loading the response information into the SoapReader object, you can use its RPCResult property to get the results. However, But rpcresult does not return directly, it returns the first entity element of
, then read the element attribute value with the text property: Reader-> rpcResult-> TextThird, an example states a simple SOAP client application: This example is made by www.xmethods.net. This server points to Yahoo online information. You can find the details at http://www.xmethods.net/VE2/viewlisting.po?serviceid=156. In the following code, you want to enter a parameter, ie Yahoo user ID. The return result is 0 to represent offline, 1 means online. See: http://www.allesta.net: 51110 / WebServices / WSDL / YahoouserPingService.xml
Four, 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
Five: Soap code in this article: #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 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: Alleesta-Yahoouserping"; connector-> beginMessage ();
// Create a SOAPSERIALIZER object SERIALIZER.CREATEINSTANCE (__ uuidof (soapserializer);
/ / Connecting Serializer-> init (_variant_t (iUnknown *) connector-> infutStream);
// Make SOAP Information Serializer-> Startenvelope (",", ""); Serializer-> StartBody (""); Serializer-> StartElement ("iSUseronline", "URI: Alleesta-Yahoouserping", "", " M "); Serializer-> StartElement (" Username ",", "," "); serializer-> WritestRing (" laghari78 "); serializer-> endelement (); serializer-> endelement (); serializer-> Endbody (); serializer-> convenvelope ();
/ / Send a message to the server connector-> endMessage ();
// Read the response Reader.createInstance (__ uuidof (soapreader));
// Connection Output READER-> LOAD (_variant_t ((iUnknown *) connectionor-> outputstream), "");
// Display result printf ("ANSWER:% S / N", (const char *) reader-> rpcResult-> text); counitialize ();
This article attached
Http://www.topxml.com/snippetcentral/snippetfiles/v20020425121357.zip