VC++使用Soap ToolKit3.0调用WebService接口
???? 1. 首先看了Soap相關資料,見http://www.w3school.com.cn/ws.asp。 調(diào)用WebService有幾種方法, 一是直接采用托管方式利用add web Reference,操作非常簡單方便,
但是貌似在VS2008中已無法實現(xiàn)了,所以并沒有做demo。
另外的方法就是非托管的,其中也包括幾種方法,第一種是采用Add web Reference實現(xiàn),同樣貌似在VS2008里無法實現(xiàn),資料:http://www.vckbase.com/index.php/wv/1408.
另外一種非托管的方法就是采用Soap ToolKit3.0 SDK實現(xiàn),這方面資料也很多,http://www.yesky.com/20020517/1611650.shtml
???? 2. demo編寫, 自己編寫的調(diào)用天氣預報的Web Service接口
#include "stdafx.h" #import <msxml4.dll> //using namespace MSXML2 #import "C:\Program Files (x86)\Common Files\MSSoap\Binaries\mssoap30.dll" named_guids \exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")using namespace MSSOAPLib30;void Add(LPWSTR cityName) { ISoapSerializerPtr Serializer; ISoapReaderPtr Reader; ISoapConnectorPtr Connector; // Connect to the service. if(FAILED(Connector.CreateInstance(__uuidof(HttpConnector30)))) //創(chuàng)建對象{wprintf(_T("failed"));}Connector->Property[_T("EndPointURL")] =_T("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"); //wsdl路徑Connector->Connect(); // Begin the message. //消息體Connector->Property[_T("SoapAction")] = _T("http://WebXml.com.cn/getWeather"); //函數(shù)體參數(shù)Connector->BeginMessage(); Serializer.CreateInstance(__uuidof(SoapSerializer30)); Serializer->Init(_variant_t((IUnknown*)Connector->InputStream)); // Build the SOAP Message.Serializer->StartEnvelope(_T("Soap"),_T(""),_T("")); Serializer->StartBody(""); Serializer->StartElement(_T("getWeather"),_T("http://WebXml.com.cn/"),_T(""),_T("Soap")); //函數(shù)處理Serializer->StartElement(_T("theCityCode"),_T(""),_T(""),_T("Soap"));Serializer->WriteString(cityName); //參數(shù)處理 Serializer->EndElement();Serializer->EndElement();Serializer->EndBody(); Serializer->EndEnvelope(); Connector->EndMessage(); Reader.CreateInstance(__uuidof(SoapReader30)); wprintf(_T("here"));Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),""); //加載返回數(shù)據(jù)// Display the result.MSXML2::IXMLDOMElementPtr pstr = Reader->RpcResult;char buff[1024] ={0};strncpy(buff,pstr->text,1024);printf("Answer:%s\n",buff); }int _tmain(int argc, _TCHAR* argv[]) {CoInitialize(NULL); Add(_T("杭州")); CoUninitialize(); getchar();return 0; }上述使用還是比較簡單和清晰的,對照著這個http://www.yesky.com/20020517/1611650.shtml資料就行了。一開始我使用 using namespace MSXML2
出現(xiàn)好多錯誤:
e:\vc工程\vcstudy\webservice\webservice\debug\mssoap30.tlh(324) : error C2872: 'IXMLDOMNode' : ambiguous symbol
1>??????? could be 'c:\program files\microsoft sdks\windows\v6.0a\include\msxml.h(531) : IXMLDOMNode'
1>??????? or?????? 'e:\vc工程\vcstudy\webservice\webservice\debug\msxml4.tlh(2837) : MSXML2::IXMLDOMNode'
最后把該命名空間的語句去掉,用手寫實現(xiàn)
MSXML2::IXMLDOMElementPtr pstr = Reader->RpcResult;
效果:
總結(jié)
以上是生活随笔為你收集整理的VC++使用Soap ToolKit3.0调用WebService接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ 多线程编程总结
- 下一篇: vs2008生成安装部署文件的步骤