+ -
当前位置:首页 → 问答吧 → 求高手帮忙 关于在vc++中使用soap

求高手帮忙 关于在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>

谢谢;

作者: 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;
}


作者: newzelg   发布时间: 2011-12-27