对soap3.0sdk研究的一点心得,具体VC6.0代码如下
#include <StdAfx.h>
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30; //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错
void query(char *EndPointURL,char *NameSpace,char *method,int argv_len,char* args[],char* argv[])
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = EndPointURL;//"http://localhost/WebService1/Service1.asmx";
Connector->Connect();//和服务器连接
// Begin message
Connector->Property["SoapAction"] = _bstr_t(NameSpace)+"/"+_bstr_t(method);//"http://xin.org/getIP";
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("soap","","");
Serializer->StartBody("body");
Serializer->StartElement(method,NameSpace,"","");//命名空间必须有
for(int i=0;i<argv_len;i++)
{
Serializer->StartElement(args[i],NameSpace,"","");
Serializer->WriteString(argv[i]);
Serializer->EndElement();
}
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the web service
Connector->EndMessage();
// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));
// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
//cout<<(const char *)Reader->RpcResult->text);
//const char * str = (const char *)Reader->RpcResult->firstChild->text;
printf("Answer: %s\n",(const char*)Reader->RpcResult->xml);
}
int main()
{ CoInitialize(NULL);
//for(int i=0;i<100;i++)
{
char *EndPointURL="http://127.0.0.1/WebService1/Service2.asmx";
char *NameSpace="http://xin.org/service2";
char *method="add";
int argv_len=2;
char* args[2]={"x","y"
};
char* argv[2]={"3","4"};
query(EndPointURL,NameSpace,method,argv_len,args,argv);
} CoUninitialize();
getchar();
return 0;
}
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30; //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错
void query(char *EndPointURL,char *NameSpace,char *method,int argv_len,char* args[],char* argv[])
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = EndPointURL;//"http://localhost/WebService1/Service1.asmx";
Connector->Connect();//和服务器连接
// Begin message
Connector->Property["SoapAction"] = _bstr_t(NameSpace)+"/"+_bstr_t(method);//"http://xin.org/getIP";
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("soap","","");
Serializer->StartBody("body");
Serializer->StartElement(method,NameSpace,"","");//命名空间必须有
for(int i=0;i<argv_len;i++)
{
Serializer->StartElement(args[i],NameSpace,"","");
Serializer->WriteString(argv[i]);
Serializer->EndElement();
}
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the web service
Connector->EndMessage();
// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));
// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
//cout<<(const char *)Reader->RpcResult->text);
//const char * str = (const char *)Reader->RpcResult->firstChild->text;
printf("Answer: %s\n",(const char*)Reader->RpcResult->xml);
}
int main()
{ CoInitialize(NULL);
//for(int i=0;i<100;i++)
{
char *EndPointURL="http://127.0.0.1/WebService1/Service2.asmx";
char *NameSpace="http://xin.org/service2";
char *method="add";
int argv_len=2;
char* args[2]={"x","y"
};
char* argv[2]={"3","4"};
query(EndPointURL,NameSpace,method,argv_len,args,argv);
} CoUninitialize();
getchar();
return 0;
}