全部博文(51)
分类: Windows平台
2013-07-10 08:29:01
#include "stdafx.h"
#include
#include
#include "tchar.h"
//需要安装 msxml.msi,把 msxml5.dll拷贝到本VS项目目录下,这样编译后的程序不依赖绝对路径,
//当然发布时也要把动态库和程序放在同一个目录下
#import "msxml5.dll"
//需要安装 SoapToolkit30.EXE,把 mssoap30.dll 拷贝到本VS项目目录下,这样编译后的程序不依赖绝对路径,
//当然发布时也要把动态库和程序放在同一个目录下
//编译后 msxml5.dll,mssoap30.dll需要拷贝到可执行程序所在的目录,程序运行时需要查找动态库
#import "mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSXML2;
using namespace MSSOAPLib30;
void query(char *EndPointURL/*,char *NameSpace,char *method,int argv_len,char* argv[],char* argn[]*/)
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
//创建DOMDocument对象
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
puts("Create IXMLDOMDocument failed\n");
return;
}
// Connect to the service
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = EndPointURL;//接口位置
hr = Connector->Connect();//和服务器连接
if ( hr != S_OK )
{
puts("connect webservice failed!\n");
return;
}
// Begin message
Connector->Property["SoapAction"] = _T("方法名
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("Soap","","");
Serializer->StartBody("");
Serializer->StartElement("getWeatherbyCityName",""); //函数处理
Serializer->StartElement("theCityName","","","Soap");
Serializer->WriteString((_bstr_t)(LPCTSTR)_T("合肥")); //参数处理
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope(); //当天气预报的webservice服务繁忙时,可能会挂着这里好长时间。
hr = Connector->EndMessage();
if ( hr != S_OK )
{
puts("connect send failed!\n");
return;
}
// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));
// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), _T(""));
pDoc->loadXML(Reader->GetRpcResult()->xml);
//在终端里输出所有拿到的天气信息
for(int i=0;i
{
printf("%s\n",(char *)(pDoc->childNodes->Getitem(0)->childNodes->Getitem(i)->Gettext()));
}
}
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
char *EndPointURL="地址,天气预报的webservice
query(EndPointURL); //执行接口调用
CoUninitialize();
getchar();
return 0;
}