1:先安装soapsdk3.0(http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe)
2:当然就是写代码
/******************主函数*******************************************************/
#include "stdafx.h"
#include "WSWrapper.h"
void main()
{
WSWrapper wsWrapper("http://www.coopur.com/WebServiceDrawMoney.asmx",
"http://tempuri.org/",
"SubmitInfo"
);//当然是调用WSWrapper.cpp,其中http://www.coopur.com/WebServiceDrawMoney.asmx是服务器端给的webservice地址。"http://tempuri.org/"是命名空间,默认的命名空间都是此地址。SubmitInfo是服务端的方法名,与客户端无关
string strName1 = "11,大大,s,dds"; //这个消息是要发送给服务端的,消息的格式是由服务端处理数据得要求决定的,本公司是要发送用户名,密码等四条信息,我以逗号分隔,服务端以逗号分隔处理数据
string strResult = wsWrapper.Hello(strName1);
printf("%s", strResult.c_str());
CoUninitialize();
}
/*****************************WSWrapper.cpp******************************************
#include "stdafx.h"
#include "WSWrapper.h"
WSWrapper::WSWrapper(const char *wsURL,
const char *wsNameSapce,
const char *wsMethodName)
: _wsURL(wsURL),
_wsNameSapce(wsNameSapce),
_wsMethodName(wsMethodName)
{
}
WSWrapper::~WSWrapper()
{
}
string WSWrapper::Hello(const string &strName)
{
try
{
HRESULT hr = CoInitialize(NULL);//初始化com环境
if(FAILED(hr))
{
//出错了
}
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
//连接到WebService,与Web服务连接
hr = Connector.CreateInstance(__uuidof(HttpConnector30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
Connector->Property["EndPointURL"] = _wsURL.c_str();
Connector->Connect();
//开始消息
Connector->Property["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();
Connector->BeginMessage();
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30)); //创建SoapSerializer对象
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));//将serializer连接到connector的输入字符串
Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");
*****下面两行我尝试删除过,也没什么问题。应该是有它的好处的,就蛮留着*********
Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");
//创建SOAP消息
Serializer->StartBody(L"NONE");
Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
Serializer->StartElement(L"strName", "http://tempuri.org/", "", "");
Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
Serializer->WriteString(strName.c_str());
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
Connector->EndMessage();//将该消息发送给web服务
//解析返回的soap包
hr = Reader.CreateInstance(__uuidof(SoapReader30)); //读取响应
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");//将reader连接到Connector的输出字符串
string strResult((const char*)Reader->RpcResult->text);
//string strResult((const char*)Reader->Body->xml);
return strResult;
}
catch(...)
{
//got a exception
}
return "error";
}
/*********************************WSWrapper.h*************************************************
#ifndef _WS_WRAPPER_H_
#define _WS_WRAPPER_H_
#import "msxml4.dll"
#import "C:/Program Files/Common Files/MSSoap/Binaries/mssoap30.dll" /
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", /
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
#include <string>
#include <Windows.h>
using namespace MSXML2;
using namespace MSSOAPLib30;
using std::string;
class WSWrapper
{
public:
WSWrapper(const char *wsURL,
const char *wsNameSapce,
const char *wsMethodName);
virtual ~WSWrapper();
string Hello(const string &strName);
private:
const string _wsURL;
const string _wsNameSapce;
const string _wsMethodName;
};
#endif