Use soap toolkit in C ++ to access web service)

Source: Internet
Author: User
In C ++, you can use soap toolkit to access web services. The following interfaces are generally used:

    • Isoapconnector
    • Isoapserializer
    • Isoapreader
These three interfaces are just as the name suggests. Isoapconnector implements the transmission protocol for sending and receiving soap messages between objects. isoapserializer is used for serialization (serialization) to create and encapsulate soap messages. isoapreader is used to read soap messages, parses a SOAP message into a DOM model and provides a series of methods to operate the DOM model. Applications using soap Toolkit Program You need to introduce the dynamic library mssoap1.dll (the latest version is soap toolkit3.0: mssoap3.dl) and msxml3.dll to parse Dom and XML. At the beginning of the program, you can introduce the relevant library and namespace: # import "msxml3.dll"
Using namespace msxml2; # import "D: \ Program Files \ common files \ mssoap \ binaries \ mssoap1.dll "\
Exclude ("istream", "isequentialstream", "_ large_integer", "_ ularge_integer", "tagstatstg", "_ filetime ")
Using namespace mssoaplib. Code Relatively real. Assume that on a Web server (such as http: // localhost/mytestwebservice/), there is a Web service named loginout and a web mothod is provided. The prototype is as follows: string logintest (string strip, int nport); the following code describes how to access the Web service. Isoapconnectorptr connector; isoapserializerptr serializer;
Isoapreaderptr reader; // connects to the Web Service
Connector. createinstance (_ uuidof (httpconnector ));
Connector-> property [L "endpointurl"] = _ variant_t ("http: // localhost/mytestwebservice/loginout. asmx ");
Hresult result = connector-> connect (); // create a soapserializer object
Serializer. createinstance (_ uuidof (soapserializer); // connect the serializer to the input string of Connector
Serializer-> Init (_ variant_t (iunknown *) connector-> inputstream); connector-> property ["soapaction"] = "http://tempuri.org/LoginTest" // start soap messages
Connector> beginmessage (); // creates a SOAP message.
Serializer-> startenvelope ("","","");
Serializer-> startbody ("");
Serializer-> startelement ("logintest", "http://tempuri.org /","","");
Serializer-> startelement ("Strip", "http://tempuri.org /","","");
Serializer-> writestring ("192.168.1.170 ");
Serializer-> endelement (); serializer-> startelement ("nport", "http://tempuri.org /","","");
Serializer-> writestring ("8000 ");
Serializer-> endelement ();
Serializer-> endelement ();
Serializer-> endbody ();
Serializer-> endenvelope (); // send the SOAP message to the Web Service
Connector-> endmessage (); // read the response SOAP message
Reader. createinstance (_ uuidof (soapreader); // connects reader to the output string of Connector
Reader-> load (_ variant_t (iunknown *) connector-> outputstream ),"");
DWORD dwrt = getlasterror ();
// Display the result
Printf ("Soap return result: % s \ n", (const char *) Reader-> rpcresult-> text );

Couninitialize ();

The above program prints the returned value of the web service. Notes:
    1. http: // localhost/mytestwebservice/loginout. asmx is the URL that provides Web Services.
    2. "http://tempuri.org/" is the namespace to which the web service belongs (the default namespace is used if the web service provider does not specify a namespace ).
    3. the soapaction when setting property is the webmethod "http://tempuri.org/LoginTest" to be called ". I think soapoperation seems to be better understood.
    4. reader-> rpcresult-> text is the string type return value that calls webmethod. To read the returned parameters or complex objects, you can use the related methods of isoapreader.
    5. In addition, according to the above method, if the relevant parameters contain Chinese characters, the corresponding content received by the Service server may be garbled! For example, when I perform a demo, the server is based on. net, the string must be UNICODE, expressed in two bytes; and the default XML encoding rule for soap toolkit is UTF-8 (using this encoding, although also called Unicode, however, Chinese characters are encoded in 2 to 3 bytes, which leads to a conflict. The solution is that when SOAP calls startenvelope (), the third parameter specifies the encoding: UTF-16 so that all characters are encoded in 2 bytes.

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.