论坛首页 入门技术论坛

使用C++开发webservice客户端(下)

浏览 2831 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-28   最后修改:2009-11-30

webserice的客户机 安装 SoapClient36g.exe,选择默认安装配置

新建vc++6.0的项目,添加下面的测试代码 TestWriteService.cpp

 

#include <stdio.h>

#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;

void main()
{
	CoInitialize(NULL);
	
	ISoapSerializerPtr Serializer;
	ISoapReaderPtr Reader;
	ISoapConnectorPtr Connector;
	
	// 与Web服务连接
	Connector.CreateInstance(__uuidof(HttpConnector));
	Connector->Property["EndPointURL"] = "http://192.168.1.99:8080/WriteRecordWS/services/WriteRecordWebService";
	Connector->Connect();
	
	// 开始消息
	Connector->BeginMessage();
	
	// 创建SoapSerializer对象
	Serializer.CreateInstance(__uuidof(SoapSerializer));
	
	// 将serializer连接到connector的输入字符串
	Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
	
	// 创建SOAP消息
	Serializer->startEnvelope("","","");
	Serializer->startBody("");

	Serializer->startElement("sayHello","","","");
	
	Serializer->startElement("in0","","","");
	Serializer->writeString("frenchmay@gmail.com");
	Serializer->endElement();

	Serializer->endElement();

	Serializer->endBody();
	Serializer->endEnvelope();

	// 将该消息发送给web服务
	Connector->EndMessage();
	
	// 读取响应
	Reader.CreateInstance(__uuidof(SoapReader));
	
	// 将reader联接到connector的输出字符串
	Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
	
	// 显示结果
	printf("Answer: %s\n", (const char *)Reader->RPCResult->text);

	Serializer.Release();
	Connector.Release();
	Reader.Release();
	CoUninitialize();
	
} 

 

上面的代码主要功能

 

1:指定webservice的网络资源路径

http://192.168.1.99:8080/WriteRecordWS/services/WriteRecordWebService

2:构建一个soap协议报文

 

	Serializer->startElement("sayHello","","","");
	
	Serializer->startElement("in0","","","");
	Serializer->writeString("c:\\123.bin");
	Serializer->endElement();

3: 通过http协议发送请求和接受响应。

	// 将该消息发送给web服务
	Connector->EndMessage();
	
	// 读取响应
	Reader.CreateInstance(__uuidof(SoapReader));

 

主义的是调试过程中,可能会因为soap协议的格式和方法参数的问题导致请求失败。

建议使用抓包工具:分别抓取java 的webservice客户端和c++的webService客户端发送的soap包,进行比较。

 

打印结果

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics