Develop a soap client using VC (method 1)

Source: Internet
Author: User
Tags soap client

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

A soap client applicationProgramExample

To illustrate how to use this articleArticleFor the soap class discussed in, we use a service listed on http://www.xmethods.net/, which can show 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.

Finally, you are most concerned about how to register DLL in soap. Soap only needs the following file: mssoap30.dll mssoapr3.dll wisc30.dll msxml4: msxml4r. DLL msxml4.dll Note: The file registration order is also listed above. Guaranteed to be usable. You can write a batch and package it together during packaging.

 

Other: http://blog.csdn.net/Tr0j4n/archive/2009/12/25/5077787.aspx

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.