#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("[email protected]"); 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();
// 将该消息发送给web服务 Connector->EndMessage(); // 读取响应 Reader.CreateInstance(__uuidof(SoapReader));
主义的是调试过程中,可能会因为soap协议的格式和方法参数的问题导致请求失败。
建议使用抓包工具:分别抓取java 的webservice客户端和c++的webService客户端发送的soap包,进行比较。