Click here to Skip to main content
15,886,012 members
Articles / Programming Languages / C++
Article

SOAP client for C++

Rate me:
Please Sign up or sign in to vote.
4.35/5 (33 votes)
31 Mar 20042 min read 1.4M   5.2K   61   99
A base class to ease the work of calling a webservice in C++.

Introduction

While creating web service and consuming it are very easy and intuitive in the .NET world, sometimes we still need to handle it in legacy systems. Several days ago, I faced a problem to call a web service in my old VC6 project. I Googled the web and realized that MS SOAP SDK will be the solution. Being familiar with C# language, going back to C++ is kind of frustrating, especially when handling COM interfaces. So I decided to write a helper class to ease the work of calling a web service, which is attached here.

The base class

The base class is called SoapClientBase, which is for inheritance only, implements most tasks to talk with the SOAP SDK. I will rather not talk a lot about the code of the base class. Instead, I'll give the steps about how to write an inherited class. The code of the base class is short, take a look at it for yourself if you want.

The inherited class

The code of the inherited class is even shorter. Here is the complete code of my class:

#pragma once
#include "SoapClientBase.h"

class AuthServiceClient : public SoapClientBase
{
public:
 AuthServiceClient(void) : SoapClientBase()
 {
  Init("<A href="http://localhost/AuthService/AuthService.asmx?wsdl">http://localhost/AuthService/AuthService.asmx?wsdl</A>", "AuthService", "");
 }

 bool IsAuthorized(LPCTSTR username, LPCTSTR password)
 {
  _variant_t varParams[2] = {  password, username };
  _variant_t varResult;
  m_hr = Invoke(L"IsAuthorized", varParams, 2, &varResult);
  return VARIANT_TRUE == varResult.boolVal;
 }
};

The signature of my webservice method (C#) is:

C#
[WebMethod]
public bool IsAuthorized(string username, string password);

As you have already seen, all that an inherited class should do is to call Init function in the constructor (or you might choose to call it explicitly outside the class), and then wrap all the web methods in local functions. You have to write all the wrap functions manually, but it's not hard to do. One thing that you should note here is that the parameter order should be reversed in the parameter array.

How to call

After you implemented the inherited class, use it to call the webservice almost the same way as you might do in C#. Here's an example:

AuthServiceClient service;
bool bResult = service.IsAuthorized(strName, strPassword);

Very simple, isn't it:)

Dependency

First of all, SOAP Toolkit3.0 SDK or SOAP Toolkit3.0 Redistributable must be installed. The base class file contains the import instruction to load the SOAP DLL. You might have to change the path if you installed the SDK in another place.

The base class does not depend on MFC nor ATL. Instead, I copied a little code from ATL to make life easier:) Therefore, you can use this class anywhere, no matter which library the application uses.

Okay, that's all. The code is very short, so don't expect too much :) The point is: it makes my life easier. And I hope it makes your life easier too. Happy programming!

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


Written By
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Comments and Discussions

 
QuestionAny full example?? Pin
my8887-Sep-06 21:42
my8887-Sep-06 21:42 
QuestionCannot read type library file Pin
wmosimann18-Aug-06 4:12
wmosimann18-Aug-06 4:12 
GeneralProblem in calling Webservice in c++ Pin
Paras Kaneriya11-Aug-06 0:13
Paras Kaneriya11-Aug-06 0:13 
GeneralRe: Problem in calling Webservice in c++ Pin
Pandele Florin29-Nov-06 22:36
Pandele Florin29-Nov-06 22:36 
GeneralProblem in calling Webservice Pin
Arslan12-Aug-06 2:41
Arslan12-Aug-06 2:41 
GeneralRe: Problem in calling Webservice Pin
Neil Yao2-Aug-06 15:04
Neil Yao2-Aug-06 15:04 
GeneralRe: Problem in calling Webservice Pin
Arslan12-Aug-06 19:24
Arslan12-Aug-06 19:24 
GeneralRe: Problem in calling Webservice Pin
raj_123k25-Oct-06 23:46
raj_123k25-Oct-06 23:46 
hi!
to remove this error add these line in ur stdAfx.h file before #endif

#import <msxml.dll> named_guids
using namespace MSXML;



Raj
GeneralRe: Problem in calling Webservice Pin
laocaicai5-Nov-06 20:44
laocaicai5-Nov-06 20:44 
GeneralRe: Problem in calling Webservice Pin
travich5-Mar-07 12:54
travich5-Mar-07 12:54 
AnswerRe: Problem in calling Webservice [modified] Pin
Rob Staveley4-Sep-07 19:31
Rob Staveley4-Sep-07 19:31 
GeneralRe: Problem in calling Webservice Pin
usman aa5-Apr-16 18:17
usman aa5-Apr-16 18:17 
GeneralRe: Problem in calling Webservice Pin
usman aa5-Apr-16 18:18
usman aa5-Apr-16 18:18 
QuestionError invoking a web service method Pin
Luca Oddi13-Jul-06 0:00
Luca Oddi13-Jul-06 0:00 
QuestionHow can I get ErrorCode from varResult Pin
edward_norway11-Jul-06 4:39
edward_norway11-Jul-06 4:39 
AnswerRe: How can I get ErrorCode from varResult Pin
Neil Yao12-Jul-06 15:32
Neil Yao12-Jul-06 15:32 
QuestionHow can I extract the response for vecotr of string Pin
arsm19-Apr-06 23:53
arsm19-Apr-06 23:53 
GeneralInvalid parameter Pin
Shan Yu3-Apr-06 20:25
Shan Yu3-Apr-06 20:25 
AnswerRe: Invalid parameter Pin
Abbas_Riazi12-Jun-06 1:10
professionalAbbas_Riazi12-Jun-06 1:10 
GeneralSoap is deprecated.... Pin
AlexEvans31-Jan-06 18:34
AlexEvans31-Jan-06 18:34 
GeneralRe: Soap is deprecated.... Pin
MJ517-Jul-06 9:24
MJ517-Jul-06 9:24 
QuestionGreat Work! Pin
fhgnne20-Jan-06 1:51
fhgnne20-Jan-06 1:51 
GeneralHELP Pin
codexploid5-Jan-06 11:59
codexploid5-Jan-06 11:59 
GeneralNice one Pin
rm_pkt24-Oct-05 2:45
rm_pkt24-Oct-05 2:45 
QuestionIf I want to return a DataSet, How to do it? Pin
Roland Liu20-Oct-05 21:20
Roland Liu20-Oct-05 21:20 

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

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