Click here to Skip to main content
11,639,922 members (59,836 online)
Click here to Skip to main content

VC Soap Client

, 7 Feb 2001 313.6K 5K 42
Rate this:
Please Sign up or sign in to vote.
Creating a SOAP client using C++ instead of VB.
<!-- Link to source file download -->
  • Download source files - 3 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 25 Kb
  • <!-- Article image -->

    Sample Image - VCSoapClient.gif

    <!-- Add the rest of your HTML here -->

    Introduction

    SOAP has a lot of potential for providing interfaces for web services. SOAP will be good for a lot of different types of services. The SOAP toolkit from Microsoft provides developers with a good starting place for learning SOAP. The SOAP Toolkit 2.0 Beta 1 was released on Jan 3, 2000. It provides new samples and a help file to explain the new interfaces. The samples provide both client and server examples to build from in developing your own services.

    One thing that is a bit neglectd in the samples (provided by Microsoft) is the use of C++ to develop the services. All of the samples are written to show the use of VB and ASP. This is fine and dandy, but from the use of SOAP my work needs to use C++. So I looked on the Internet for examples of SOAP using VC. To my surprise, I only found one, and it used the SOAP Toolkit 1.0 instead of the latest 2.0.

    Since the VB samples worked perfectly fine, I decided to develop a client application using VC to talk to the VB Web Service. I started with the Low-Level VB Calculator program and the VC sample that I had from the SOAP Toolkit 1.0.

    To include the COM interfaces from the SOAP DLLs, the #import directive was used. This was used for the MSSOAP1.dll and a using clause was added to use the MSSOAPLib namespace. The VB sample used a WinInetConnector to connect to the webserver for the service. This interface is not in the MSSOAPLib namespace. WiSC10.dll was also imported so that I had the connector that was needed.

    This second import led to problems. I had never had to import more than one DLL in any of my previous projects. I was getting a fatal error LNK1179: invalid or corrupt file: duplicate comdat "_IID_ISoapConnector" link error. After some research, I found that I had to remove the named_guids from the second import line. This solved my linking error and allowed the rest of the coding to go a lot smoother. An exclude statement was included to remove some compiler warnings about automatic excludes.

    // Replace with the path of SOAP DLLs 
    // Exclude was added to remove the automatic exclusions
    #import "C:\\Program Files\\MSSoapSDK\\Binaries\\MSSOAP1.dll" named_guids raw_interfaces_only exclude("IStream","ISequentialStream","_LARGE_INTEGER","_ULARGE_INTEGER","tagSTATSTG","_FILETIME")
    #import "C:\\Program Files\\MSSoapSDK\\Binaries\\WiSC10.dll" raw_interfaces_only exclude("IStream","ISequentialStream","_LARGE_INTEGER","_ULARGE_INTEGER","tagSTATSTG","_FILETIME")
    using namespace MSSOAPLib;
    

    From here, the rest of the conversion from the VB code to the C++ interfaces was simple. This was fairly straight forward for anyone that has done conversions before. Smart pointers were used to make the creating and releasing of objects simpler.

    SOAP Client Summary

    For this VC example, I decided to use the one VC sample that I had found online as a base for development. The problem with this is that it was a console application and not a windowed application. I decided to still develop it using the console based app and just stick with one type of command to the server. Including this code into a normal Window application should be a trivial thing for most VC COM developers.

    To begin this low-level SOAP client, you need to create a SoapConnector. The implementation of the SoapConnector could use any type of transport protocol such as HTTP, FTP, or SMTP. The SOAP documentation says that there are three different SoapConnector implementations included with the SOAP Toolkit 2.0. These implementations are WinInetConnector, XmlHttpConnector (default for 9x/Me clients), and HttpLibConnector (default for NT/2K clients). I choose to use the WinInetConnector since the VB sample that I was basing my code on used it. I went back to try the other implemetations of the SoapConnector and all of them worked correctly on Win2k.

    	// Create the Connector
    	Connector = NULL;
    	Connector.CreateInstance(__uuidof(WinInetConnectorLib::WinInetConnector));
    
    	// Connect to the web service
    	Connector->put_Property(endpoint,vEndPoint);
    	Connector->Connect(NULL);
    
    	// Begin the message to the server
    	Connector->put_Property(action,vAction);
    	Connector->BeginMessage(NULL);
    

    After creating the SoapConnector, the EndPointURL was set and the Connect method was called. This allows the code to connect to the web service. The SoapAction was then set to uri:Multiply to tell the server what action I was going to be requesting. This follows the VB example from the toolkit.

    The next step is to create the SoapSerializer. The SoapSerializer is the interface that allows you to create the XML message and send it to the server.

    	// Create the SoapSerializer
    	Serializer = NULL;
    	Serializer.CreateInstance(__uuidof(SoapSerializer));
    
    	// Connect the serializer to the input stream of the connector
    	Connector->get_InputStream(&inputstream);
    	_variant_t stream = inputstream;
    	Serializer->Init(stream);
    
    	// Build the XML message manually
    	Serializer->startEnvelope(NULL,NULL,NULL);
    	Serializer->startBody(NULL);
    	Serializer->startElement(method,command,NULL,m);
    	Serializer->startElement(a,NULL,NULL,NULL);
    	Serializer->writeString(val1);
    	Serializer->endElement();
    	Serializer->startElement(b,NULL,NULL,NULL);
    	Serializer->writeString(val2);
    	Serializer->endElement();
    	Serializer->endElement();
    	Serializer->endBody();
    	Serializer->endEnvelope();
    		
    	// Send the message to the web service
    	Connector->EndMessage();		
    

    After the SoapSerializer is created, it needs to be attached to the input stream of the SoapConnector. After this, the message is created and sent. As an interesting note, as you look over the source code, the EndMessage method is the command that sends the message to the server.

    Up to this point, we have built the XML command and sent it to the server to be executed. The next step is to read the results. I left error checking off for this, but it can be added. To read the reply from the server the client application needs to use a SoapReader. The SoapReader is connected to the output stream of the SoapConnector. From there, the results can be read back in using an IXMLDOMElement object. This is then displayed using std::cout.

    	// Create the SoapReader
    	Reader = NULL;
    	Reader.CreateInstance(__uuidof(SoapReader));
    
    	// Connect the reader to the output stream of the connector
    	Connector->get_OutputStream(&outputstream);
    	_variant_t outstream = outputstream;
    	VARIANT_BOOL bres;
    	Reader->load(outstream,&bres);
    
    	// Get the results from the server and print it
    	MSSOAPLib::IXMLDOMElement *element;
    	Reader->get_RPCResult(&element);
    	BSTR buff;
    	element->get_baseName(&buff);
    	std::cout << W2A(buff) << std::endl;
    	element->get_text(&buff);
    	std::cout << W2A(buff) << std::endl;
    

    Conclusion

    Overall, SOAP communications is a simple thing for most developments. The low-level communications shown here allows you to see the creation of the XML message as well as parse out the return message. This is a simple example based on the ClcLVBCl sample that comes with the SOAP Toolkit 2.0 that shows the way that SOAP can be used with VC.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    Steve Maier
    Software Developer (Senior) Microsoft
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

     
    QuestionAccept-Encoding Pin
    sercinyou17-Dec-08 0:01
    membersercinyou17-Dec-08 0:01 
    GeneralCould you help me in writing SOAP API in C++ Pin
    msurni5-Mar-08 19:36
    membermsurni5-Mar-08 19:36 
    GeneralVc++ Clietn .NET C# webservices Pin
    Kabirdas Jaunjare7-Jun-07 20:24
    memberKabirdas Jaunjare7-Jun-07 20:24 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    Steve Maier8-Jun-07 6:22
    memberSteve Maier8-Jun-07 6:22 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    Kabirdas Jaunjare9-Jun-07 19:44
    memberKabirdas Jaunjare9-Jun-07 19:44 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    Steve Maier10-Jun-07 17:07
    memberSteve Maier10-Jun-07 17:07 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    Kabirdas Jaunjare10-Jun-07 19:13
    memberKabirdas Jaunjare10-Jun-07 19:13 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    Kabirdas Jaunjare12-Jun-07 0:08
    memberKabirdas Jaunjare12-Jun-07 0:08 
    GeneralRe: Vc++ Clietn .NET C# webservices Pin
    jstevenco27-Nov-07 14:54
    memberjstevenco27-Nov-07 14:54 
    GeneralAuth Error in EndMessage() Pin
    Amit Shirsikar29-Sep-05 5:38
    sussAmit Shirsikar29-Sep-05 5:38 
    GeneralSOAP Client - Create Instance problem - invalid pointer Pin
    leventozgur13-Sep-05 22:24
    memberleventozgur13-Sep-05 22:24 
    GeneralRe: SOAP Client - Create Instance problem - invalid pointer Pin
    Steve Maier14-Sep-05 16:38
    memberSteve Maier14-Sep-05 16:38 
    QuestionSOAP response is ?????? Pin
    atulvij1930-Aug-05 11:29
    memberatulvij1930-Aug-05 11:29 
    GeneralPassing NULL value to WebService Pin
    A.Ravikumar8-Aug-05 2:03
    memberA.Ravikumar8-Aug-05 2:03 
    GeneralRe: Passing NULL value to WebService Pin
    Kabirdas Jaunjare7-Jun-07 20:13
    memberKabirdas Jaunjare7-Jun-07 20:13 
    GeneralProblem with SOAP-ENV:Header with mssoap1.dll Pin
    smpshehan18-May-05 19:41
    membersmpshehan18-May-05 19:41 
    GeneralRe: Problem with SOAP-ENV:Header with mssoap1.dll Pin
    Steve Maier19-May-05 3:04
    memberSteve Maier19-May-05 3:04 
    GeneralRe: Problem with SOAP-ENV:Header with mssoap1.dll Pin
    smpshehan19-May-05 15:53
    membersmpshehan19-May-05 15:53 
    GeneralRe: Problem with SOAP-ENV:Header with mssoap1.dll Pin
    nitincop9-Mar-08 20:09
    membernitincop9-Mar-08 20:09 
    GeneralMSSOAP1.dll program problem in VC++ Pin
    smpshehan18-May-05 19:33
    membersmpshehan18-May-05 19:33 
    GeneralProblem with SOAP-ENV:Header with mssoap1.dll Pin
    smpshehan18-May-05 19:28
    membersmpshehan18-May-05 19:28 
    GeneralRe: Problem with SOAP-ENV:Header with mssoap1.dll Pin
    Amit Shirsikar29-Sep-05 5:34
    sussAmit Shirsikar29-Sep-05 5:34 
    GeneralQuestion Pin
    reema13-Apr-05 19:28
    memberreema13-Apr-05 19:28 
    GeneralRe: Question Pin
    Steve Maier4-Apr-05 4:25
    memberSteve Maier4-Apr-05 4:25 
    GeneralSOAP demo Pin
    PierreCloutier21-Feb-05 14:45
    memberPierreCloutier21-Feb-05 14:45 
    GeneralRe: SOAP demo Pin
    suen09313-Sep-08 19:07
    membersuen09313-Sep-08 19:07 
    GeneralRe: SOAP demo Pin
    Steve Maier4-Sep-08 2:23
    memberSteve Maier4-Sep-08 2:23 
    GeneralRe: SOAP demo Pin
    suen09314-Sep-08 2:59
    membersuen09314-Sep-08 2:59 
    QuestionHELP ME!!!why toolkit3.0 generate these bad XML for me? Pin
    gxulg16-Jan-05 19:13
    membergxulg16-Jan-05 19:13 
    AnswerRe: HELP ME!!!why toolkit3.0 generate these bad XML for me? Pin
    Steve Maier17-Jan-05 4:05
    memberSteve Maier17-Jan-05 4:05 
    GeneralVC Sopa Client ( SSL on tomcat ) Pin
    saravanababu16-Mar-04 19:33
    membersaravanababu16-Mar-04 19:33 
    QuestionSerialize file through HTTP?? Pin
    xxhimanshu30-Oct-03 19:40
    memberxxhimanshu30-Oct-03 19:40 
    QuestionWeb service security ??? Pin
    shalender1-May-03 2:31
    membershalender1-May-03 2:31 
    GeneralDebugging DLL Pin
    Manish K. Agarwal22-Apr-03 1:43
    memberManish K. Agarwal22-Apr-03 1:43 
    QuestionHow to pass to this URL Pin
    Anthony_Yio22-Apr-03 1:34
    memberAnthony_Yio22-Apr-03 1:34 
    AnswerRe: How to pass to this URL Pin
    Anthony_Yio18-May-03 16:12
    memberAnthony_Yio18-May-03 16:12 
    Questionhow about EVC++3.0 Pin
    sesham21-Aug-02 12:43
    membersesham21-Aug-02 12:43 
    Generalhello Pin
    Anonymous15-Aug-02 23:08
    sussAnonymous15-Aug-02 23:08 
    Generalupdated source code Pin
    Anonymous18-Apr-02 21:01
    memberAnonymous18-Apr-02 21:01 
    GeneralRe: updated source code Pin
    Lei Cheng26-Sep-02 21:39
    sussLei Cheng26-Sep-02 21:39 
    GeneralRe: updated source code Pin
    lei cheng26-Sep-02 22:55
    susslei cheng26-Sep-02 22:55 
    GeneralVC Soap Pin
    Ahmad Al Ali10-Jul-01 10:25
    memberAhmad Al Ali10-Jul-01 10:25 
    GeneralRe: VC Soap Pin
    Steve Maier10-Jul-01 10:28
    memberSteve Maier10-Jul-01 10:28 
    GeneralRe: VC Soap Pin
    Sekar27-Aug-01 2:51
    memberSekar27-Aug-01 2:51 
    GeneralRe: VC Soap Pin
    Slare27-Aug-01 21:52
    memberSlare27-Aug-01 21:52 
    GeneralRe: VC Soap Pin
    Mclaughm14-Nov-01 11:32
    memberMclaughm14-Nov-01 11:32 
    GeneralRe: VC Soap Pin
    chenzhu18-Nov-01 4:27
    memberchenzhu18-Nov-01 4:27 
    GeneralRe: VC Soap Pin
    Jabran Asghar25-Feb-02 5:02
    memberJabran Asghar25-Feb-02 5:02 
    GeneralRe: VC Soap Pin
    Nunny18-Mar-02 22:42
    memberNunny18-Mar-02 22:42 
    GeneralRe: VC Soap Pin
    Anonymous8-May-02 7:20
    memberAnonymous8-May-02 7:20 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150731.1 | Last Updated 8 Feb 2001
    Article Copyright 2001 by Steve Maier
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid