分类: C/C++
2009-05-02 22:20:12
// SoapClientWrap.h: interface for the CSoapClientWrap
class.
//
//////////////////////////////////////////////////////////////////////
#if
!defined(AFX_SOAPCLIENTWRAP_H__9C0644A3_B96D_47CF_B062_0D07A3F88998__INCLUDED_)
#define
AFX_SOAPCLIENTWRAP_H__9C0644A3_B96D_47CF_B062_0D07A3F88998__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER >
1000
#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;
typedef struct
{
char *
sParamName;
char * sParamValue;
}WSPARAM;
//WSPARAM
wsParam[20];
class CSoapClientWrap
{
public:
bool Invoke(char *
wsURL,char * wsSoapAction,char * wsNameSpace,char * wsMethod,WSPARAM
*wsParam,int wsCount,char * sEmsg,CString * sRmsg,bool
wsXML);//sEmsg,出错消息,sRmsg,返回数据
CSoapClientWrap();
virtual
~CSoapClientWrap();
protected:
ISoapConnectorPtr
SoapConnector;
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
};
#endif //
!defined(AFX_SOAPCLIENTWRAP_H__9C0644A3_B96D_47CF_B062_0D07A3F88998__INCLUDED_)
////CPP
// SoapClientWrap.cpp: implementation of the CSoapClientWrap
class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "WebService.h"
#include
"SoapClientWrap.h"
#include "..\\ChineseCodeLib.h"
#include
using namespace std;
#ifdef _DEBUG
#undef
THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new
DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//
Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSoapClientWrap::CSoapClientWrap()
{
}
CSoapClientWrap::~CSoapClientWrap()
{
try
{
// CoUninitialize();
}
catch(...)
{
;
}
}
//函数名
Invoke(char *wsURL,char * wsSoapAction,char *wsNameSpace, char *wsMethod,
WSPARAM *wsParam, int wsCount,char * sEmsg,char *
sRmsg)
//说明:按指定属性访问远程的WEBSERVICE,并返回给调用函数
//参数说明:
//
wsURL:WEBSERVICE的链接地址
// wsSoapAction:在页面中可访问到的SoapAction;
//
wsNameSpace:在页面中定义的命名空间,一般地使用:
//
wsMethod:要访问的WEBSERVICE 方法名
// wsParam: 要使用的参数,见WSPARAM定义,
// wsCount:
参数总数
// sEmsg://要返回给调用函数的错误码率信息
//
sRmsg://要返回的捃执行结果信息
//返回值:
//
如果正常执行返回true,中间有问题就返回false;
//李浩
//2007-3-21日
//编写说明:
//为了让返回信息及调用信息一致,故将CoInitalize也在Invoke中
//
wsXML:要返回的是XML值还是TEXT
bool CSoapClientWrap::Invoke(char *wsURL,char * wsSoapAction,char
*wsNameSpace, char *wsMethod, WSPARAM *wsParam, int wsCount,char * sEmsg,CString
* sRmsg,bool wsXML)
{
HRESULT hr;
hr=
CoInitialize(NULL);//初始化com环境
if(FAILED(hr))
{
strcpy(sEmsg,"初始化COM失败");
return
false;
}
hr =
SoapConnector.CreateInstance(__uuidof(HttpConnector));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
strcpy(sEmsg,"创建HttpConnector
FAILED");
return false;
}
SoapConnector->Property["EndPointURL"]
= (LPTSTR)(LPCTSTR)wsURL;
hr = SoapConnector->Connect();
if
(FAILED(hr))
{
strcpy(sEmsg,"连接WEBSERVICE FAILED");
return
false;
}
SoapConnector->Property["SoapAction"] =
(LPTSTR)(LPCTSTR)wsSoapAction;
//保持为空字符串即可
SoapConnector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer));
//
将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)SoapConnector->InputStream));
Serializer->startEnvelope("","","");
//Serializer->startEnvelope("","","");
Serializer->startBody("NONE");
//要调用的函数名
Serializer->startElement(wsMethod,wsNameSpace,"NONE","");
//以下是为函数加入要调用的参数
for(int
i =0;i
WSPARAM wsparam =
wsParam[i];
Serializer->startElement(wsparam.sParamName,wsNameSpace,"NONE","");
string
UTFValue;
CChineseCodeLib
myCode;
myCode.GB2312ToUTF_8(UTFValue,wsparam.sParamValue,strlen(wsparam.sParamValue));
Serializer->writeString(UTFValue.c_str());
Serializer->endElement();
}
Serializer->endElement();
Serializer->endBody();
Serializer->endEnvelope();
try
{
SoapConnector->EndMessage();
}
catch(...)
{
strcpy(sEmsg,"提交SOAP消息出错");
return
false;
}
hr = Reader.CreateInstance(__uuidof(SoapReader));
if
(FAILED(hr))
{
strcpy(sEmsg,"CreateInstance READER
FAILED");
return
false;
}
Reader->Load(_variant_t((IUnknown*)SoapConnector->OutputStream),
"");
if (wsXML)
sRmsg->Format("%s",(const
char*)Reader->RPCResult->xml);
//strcpy(sRmsg,);
else
sRmsg->Format("%s",(const
char*)Reader->RPCResult->text);
//strcpy(sRmsg,(const
char*)Reader->RPCResult->text);
return true;
}