vc开发soap客户端(方式二)

 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:

vc开发soap客户端(方式二) Collapse
#pragma once
#include "SoapClientBase.h"


class AuthServiceClient : public SoapClientBase
{
public:
AuthServiceClient(void) : SoapClientBase()
{
Init("http://localhost/AuthService/AuthService.asmx?wsdl">http://localhost/AuthService/AuthService.asmx?wsdl", "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:

vc开发soap客户端(方式二) Collapse
 [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:

vc开发soap客户端(方式二) Collapse
 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

(本文作者:Neil Yao)

类库下载

你可能感兴趣的:(SOAP)