VC6.0下调用Webservice

来源:互联网 发布:情系探花头像psd源码 编辑:程序博客网 时间:2024/06/11 16:12


1、先安装soapsdk3.0(http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe)
2、编写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

3、编写WSWrapper.cpp

#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
 {
  printf("2\n");
  HRESULT hr = CoInitialize(NULL);//初始化com环境
  if(FAILED(hr))
  {
   //出错了
  }
  ISoapSerializerPtr Serializer;
  ISoapReaderPtr Reader;
  ISoapConnectorPtr Connector;
  //连接到WebService
  hr = Connector.CreateInstance(__uuidof(HttpConnector30));
  printf("3\n");
  if(FAILED(hr))
  {
   //创建com对象出错,一般是因为没有安装com
  }
  Connector->Property["EndPointURL"] = _wsURL.c_str(); 
  Connector->Connect();
  Connector->Property["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();        
  //开始创建webservice的请求Soap包
  Connector->BeginMessage();
  printf("4\n");
  hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
  printf("5\n");
  if(FAILED(hr))
  {
   //创建com对象出错,一般是因为没有安装com
   printf("failed!\n");
   return "";
  }
  Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
  printf("51\n");
  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");
  Serializer->StartBody(L"NONE");
  printf("52\n");
  Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
  Serializer->StartElement(L"strName", "", "NONE", "");
  Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
  Serializer->WriteString(strName.c_str());    
  printf("53\n");
  Serializer->EndElement();
  Serializer->EndElement();
  Serializer->EndBody();
  printf("54\n");
  Serializer->EndEnvelope();
  printf("55\n");
  Connector->EndMessage();
  printf("6\n");
  //解析返回的soap包
  hr = Reader.CreateInstance(__uuidof(SoapReader30));
  printf("7\n");
  if(FAILED(hr))
  {
   //创建com对象出错,一般是因为没有安装com
  }
  Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
  printf("8\n");
  string strResult((const char*)Reader->RpcResult->text);
  return strResult;
 }
 catch(_com_error e)
 {            
  //got a exception
  printf(e.Description());
  printf("\n");
  printf("FAILED!");
  return "error";
 }
}

4、Webservice 测试例子:testWebservice.cpp

#include "stdafx.h"
#include "WSWrapper.h"

int main(int argc, char* argv[])
{
 printf("test...\n");
 WSWrapper wsWrapper("http://localhost/TestWebService/Service1.asmx",
  "http://localhost/TestWebService/",
  "Hello");
 printf("1\n");
 string strName = "Lucy";
 string strResult = wsWrapper.Hello(strName);
 printf("11\n");
 printf("%s", strResult.c_str());
 getchar();
 return 0;
}

原创粉丝点击