1)安装soap toolkit3.0 ,这个可以比较方便的在网上下载到
2)程序开发
#import "msxml4.dll"
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
//strServer:webservice服务器地址 strMethod:方法名 arrParm:参数表(参数名1,参数值1,参数名2,参数值2 。。。) strRet:返回的结果串
HRESULT WebServiceCall(CString strServer, CString strMethod, CArray<CString, CString>& arrParam, CString& strRet)
{
HRESULT hRet = FALSE;
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
//CComQIPtr<ISoapReader> Reader;
//CComQIPtr<ISoapConnector> Connector;
try
{
HRESULT hr;
hr = Connector.CreateInstance(__uuidof(HttpConnector30));
if( FAILED(hr) )
{
//AfxMessageBox("\nSoap 功能缺失! -请确认您的机器上安装有soap3.0或3.0以上版本补丁包, 以及msxml4.0或4.0以上的版本");
TRACE("\nSoap 功能缺失! -请确认您的机器上安装有soap3.0或3.0以上版本补丁包, 以及msxml4.0或4.0以上的版本\n");
return FALSE;
}
Connector->Property["EndPointURL"] = strServer.GetBuffer(0);// szUrl;
Connector->Property["Timeout"]="15000";
//Connector->Property["Timeout"]="3000";
Connector->Property["ProxyServer"]="<CURRENT_USER>"; //使用浏览器的代理设置
hr = Connector->Connect();
Connector->Property["SoapAction"] = strMethod.GetBuffer(0);
Connector->BeginMessage();
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
if( FAILED(hr) )
{
//AfxMessageBox("\nSoap 功能缺失!请确认您的机器上安装有soap3.0或3.0以上版本补丁包, 以及msxml4.0或4.0以上的版本");
TRACE("\nSoap 功能缺失!请确认您的机器上安装有soap3.0或3.0以上版本补丁包, 以及msxml4.0或4.0以上的版本\n");
return FALSE;
}
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
CString strTmp;
strTmp.Format("%s?method=%s", strServer, strMethod );
Serializer->StartElement(strMethod.GetBuffer(0), strTmp.GetBuffer(0), "", "");
//循环写入参数
for(int i=0; i<arrParam.GetSize(); i+=2)
{
//CString sss1 = arrParam[i];
//CString sss2 = arrParam[i+1];
Serializer->StartElement(arrParam[i].GetBuffer(0), "", "", "" );
Serializer->WriteString(arrParam[i+1].GetBuffer(0));
Serializer->EndElement();
}
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
hr = Connector->EndMessage();
Connector->InputStream;
hr = Reader.CreateInstance(__uuidof(SoapReader30));
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");//
strRet = (const char*)Reader->RpcResult->text;
hRet = TRUE;
//AfxMessageBox(strRet);
}
catch (_com_error e)
{
//CString strErrMsg;
//strErrMsg.Format("接口调用失败:%s", strMethod);
//AfxMessageBox(strErrMsg);
TRACE("接口调用失败:%s\n", strMethod);
return FALSE;
}
catch (...)
{
return FALSE;
}
return hRet;
}
3)程序发布
编译好的exe程序要发布给最终用户,用户很可能没有安装soap toolkit3,所以再发布的时候必须把相关的dll一起打包,并且进行注册。
需要一起打包的dll有5个, MSSOAPR3.dll,MSSOAP30.dll,WISC30.dll,msxml4r.dll, msxml4.dll
完成注册
regsvr32 /s MSSOAPR3.dll
regsvr32 /s MSSOAP30.dll
regsvr32 /s WISC30.dll
regsvr32 /s msxml4.dll
特别要注意,只需要注册4个dll,注册的顺序好像不能随便改动。msxml4r.dll不需要注册,注册也不会成功,但是必须把这个dll和 msxml4.dll放一起发布,否则 msxml4.dll无法注册。