求高手帮忙 关于在vc++中使用soap
时间:2011-12-19
来源:互联网
如题,怎么在vc++里实现
<soap-env:body>
<Information
xmlns="http://www.xxxx.com">
<userid
xmlns:SOAPSDK5="http://www.xxxx.com">
123
</userid>
<username
xmlns:SOAPSDK5="http://www.xxxx.com">
123
</username>
</userinformation>
</soap-env:body>
谢谢;
<soap-env:body>
<Information
xmlns="http://www.xxxx.com">
<userid
xmlns:SOAPSDK5="http://www.xxxx.com">
123
</userid>
<username
xmlns:SOAPSDK5="http://www.xxxx.com">
123
</username>
</userinformation>
</soap-env:body>
谢谢;
作者: icebridge036 发布时间: 2011-12-19
//异步调用asmx或者wsdl
//申明全局变量
XMLHttpEventSink* p_sink;
IXMLHTTPRequestPtr m_pIXMLHTTPRequest;
#import "msxml6.dll"
using namespace MSXML2;
CTestWs::CTestWs(){
CoInitialize(NULL);
HRESULT hr=m_pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
p_sink=NULL;
}
CTestWs::~CTestWs()
{
m_pIXMLHTTPRequest.Release();
m_pIXMLHTTPRequest=NULL;
if(!p_sink){
delete p_sink;
p_sink=NULL;
}
CoUninitialize();
}
BOOL CTestWs::wsTest(CString mParam1){
HRESULT hr;
BOOL bResult=TRUE;
tagVARIANT bAsync;
bAsync.vt = VT_BOOL;
bAsync.boolVal = TRUE;
try {
hr=m_pIXMLHTTPRequest->open("POST","http://.../ws.asmx", bAsync);
SUCCEEDED(hr) ? 0 : throw hr;
m_pIXMLHTTPRequest->setRequestHeader("Content-Type","text/xml; charset=UTF-8");
m_pIXMLHTTPRequest->setRequestHeader("SOAPAction","http://.....");
CString soapData;
// Set the soap data
soapData.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
<soap:Body>\
<WsMethod>\
<Param1>%s</Param1>\
<dq>%s</dq>\
</WsMethod>\
</soap:Body>\
</soap:Envelope>",
mParam1);
p_sink = new XMLHttpEventSink(m_pIXMLHTTPRequest,this);
m_pIXMLHTTPRequest->put_onreadystatechange((IDispatch *)p_sink);
hr=m_pIXMLHTTPRequest->send((LPCTSTR)soapData);
SUCCEEDED(hr) ? 0 : throw hr;
} catch (...) {
bResult=FALSE;
}
return bResult;
}
//XMLHttpEventSink类
#ifndef XMLHTTPEVENTSINK_H_
#define XMLHTTPEVENTSINK_H_
#include <wtypes.h>
#include <windows.h>
#import "msxml6.dll"
using namespace MSXML2;
class CZjnewdXmlClient;
class XMLHttpEventSink : public IDispatch
{
public:
XMLHttpEventSink(IXMLHTTPRequestPtr request,CZjnewdXmlClient* pClient):
_refCount(1), _request(request),m_pZjnewdXmlClient(pClient)
{
// Don't increase the reference count to the request object; doing so would create a circular reference
// and thus a memory leak.
}
virtual ~XMLHttpEventSink(){}
// IUnknown
STDMETHODIMP QueryInterface(const IID &riid, void **ppvObject);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// IDispatch
STDMETHODIMP GetTypeInfoCount(UINT *pctinfo);
STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
STDMETHODIMP GetIDsOfNames(const IID &riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
STDMETHODIMP Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
private:
LONG _refCount;
IXMLHTTPRequestPtr _request;
HANDLE _completedEvent;
CZjnewdXmlClient* m_pZjnewdXmlClient;
};
#endif
//.cpp
#include "stdafx.h"
#include "XMLHttpEventSink.h"
#include "ZjnewdXmlClient.h"
STDMETHODIMP XMLHttpEventSink::QueryInterface(const IID &riid, void **ppvObject)
{
if( ppvObject == NULL )
return E_INVALIDARG;
*ppvObject = NULL;
if( riid == IID_IUnknown )
*ppvObject = static_cast<IUnknown*>(this);
else if( riid == IID_IDispatch )
*ppvObject = static_cast<IDispatch*>(this);
if( *ppvObject == NULL )
return E_NOINTERFACE;
AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) XMLHttpEventSink::AddRef()
{
return InterlockedIncrement(&_refCount);
}
STDMETHODIMP_(ULONG) XMLHttpEventSink::Release()
{
LONG refCount = InterlockedDecrement(&_refCount);
if( refCount == 0 )
{
delete this;
return 0;
}
else
return refCount;
}
STDMETHODIMP XMLHttpEventSink::GetTypeInfoCount(UINT *pctinfo)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::GetIDsOfNames(const IID &riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
// Since this class isn't used for anything else, Invoke will get called only for onreadystatechange, and
// dispIdMember will always be 0.
long state;
// Retrieve the state
_request->get_readyState(&state);
if (state == 4)
{
// The request has completed.
// Get the request status.
long status;
_request->get_status(&status);
if( status == 200 )
{
// Get the response body if we were successful.
IXMLDOMDocumentPtr pXMLDoc=NULL;
BSTR bstrString = NULL;
CString strResult;
//pXMLDoc=_request->responseXML;
//bstrString=pXMLDoc->childNodes->item[1]->text;
//strResult=bstrString;
BSTR body;
_request->get_responseText(&body);
strResult=body;
this->Release();
}
// Signal the main thread we're done.
//SetEvent(_completedEvent);
}
return S_OK;
}
//申明全局变量
XMLHttpEventSink* p_sink;
IXMLHTTPRequestPtr m_pIXMLHTTPRequest;
#import "msxml6.dll"
using namespace MSXML2;
CTestWs::CTestWs(){
CoInitialize(NULL);
HRESULT hr=m_pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
p_sink=NULL;
}
CTestWs::~CTestWs()
{
m_pIXMLHTTPRequest.Release();
m_pIXMLHTTPRequest=NULL;
if(!p_sink){
delete p_sink;
p_sink=NULL;
}
CoUninitialize();
}
BOOL CTestWs::wsTest(CString mParam1){
HRESULT hr;
BOOL bResult=TRUE;
tagVARIANT bAsync;
bAsync.vt = VT_BOOL;
bAsync.boolVal = TRUE;
try {
hr=m_pIXMLHTTPRequest->open("POST","http://.../ws.asmx", bAsync);
SUCCEEDED(hr) ? 0 : throw hr;
m_pIXMLHTTPRequest->setRequestHeader("Content-Type","text/xml; charset=UTF-8");
m_pIXMLHTTPRequest->setRequestHeader("SOAPAction","http://.....");
CString soapData;
// Set the soap data
soapData.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
<soap:Body>\
<WsMethod>\
<Param1>%s</Param1>\
<dq>%s</dq>\
</WsMethod>\
</soap:Body>\
</soap:Envelope>",
mParam1);
p_sink = new XMLHttpEventSink(m_pIXMLHTTPRequest,this);
m_pIXMLHTTPRequest->put_onreadystatechange((IDispatch *)p_sink);
hr=m_pIXMLHTTPRequest->send((LPCTSTR)soapData);
SUCCEEDED(hr) ? 0 : throw hr;
} catch (...) {
bResult=FALSE;
}
return bResult;
}
//XMLHttpEventSink类
#ifndef XMLHTTPEVENTSINK_H_
#define XMLHTTPEVENTSINK_H_
#include <wtypes.h>
#include <windows.h>
#import "msxml6.dll"
using namespace MSXML2;
class CZjnewdXmlClient;
class XMLHttpEventSink : public IDispatch
{
public:
XMLHttpEventSink(IXMLHTTPRequestPtr request,CZjnewdXmlClient* pClient):
_refCount(1), _request(request),m_pZjnewdXmlClient(pClient)
{
// Don't increase the reference count to the request object; doing so would create a circular reference
// and thus a memory leak.
}
virtual ~XMLHttpEventSink(){}
// IUnknown
STDMETHODIMP QueryInterface(const IID &riid, void **ppvObject);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// IDispatch
STDMETHODIMP GetTypeInfoCount(UINT *pctinfo);
STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
STDMETHODIMP GetIDsOfNames(const IID &riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
STDMETHODIMP Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
private:
LONG _refCount;
IXMLHTTPRequestPtr _request;
HANDLE _completedEvent;
CZjnewdXmlClient* m_pZjnewdXmlClient;
};
#endif
//.cpp
#include "stdafx.h"
#include "XMLHttpEventSink.h"
#include "ZjnewdXmlClient.h"
STDMETHODIMP XMLHttpEventSink::QueryInterface(const IID &riid, void **ppvObject)
{
if( ppvObject == NULL )
return E_INVALIDARG;
*ppvObject = NULL;
if( riid == IID_IUnknown )
*ppvObject = static_cast<IUnknown*>(this);
else if( riid == IID_IDispatch )
*ppvObject = static_cast<IDispatch*>(this);
if( *ppvObject == NULL )
return E_NOINTERFACE;
AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) XMLHttpEventSink::AddRef()
{
return InterlockedIncrement(&_refCount);
}
STDMETHODIMP_(ULONG) XMLHttpEventSink::Release()
{
LONG refCount = InterlockedDecrement(&_refCount);
if( refCount == 0 )
{
delete this;
return 0;
}
else
return refCount;
}
STDMETHODIMP XMLHttpEventSink::GetTypeInfoCount(UINT *pctinfo)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::GetIDsOfNames(const IID &riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
return E_NOTIMPL;
}
STDMETHODIMP XMLHttpEventSink::Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
// Since this class isn't used for anything else, Invoke will get called only for onreadystatechange, and
// dispIdMember will always be 0.
long state;
// Retrieve the state
_request->get_readyState(&state);
if (state == 4)
{
// The request has completed.
// Get the request status.
long status;
_request->get_status(&status);
if( status == 200 )
{
// Get the response body if we were successful.
IXMLDOMDocumentPtr pXMLDoc=NULL;
BSTR bstrString = NULL;
CString strResult;
//pXMLDoc=_request->responseXML;
//bstrString=pXMLDoc->childNodes->item[1]->text;
//strResult=bstrString;
BSTR body;
_request->get_responseText(&body);
strResult=body;
this->Release();
}
// Signal the main thread we're done.
//SetEvent(_completedEvent);
}
return S_OK;
}
作者: newzelg 发布时间: 2011-12-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28