突然要在c++里调用webservice,一时还不知道从何下手,又想起了.net的好了,直接用wsdl命令生成一个代理类就搞定了,于是又开始了在网上寻觅的历程。这年代没有google,估计要少活10年。
搜索"vc6"+Webservice,出来了一大堆,不过内容基本上一样(又让我体会了一把“天下文章一大抄”的经典论据,到头来都不知道谁抄谁)也有博客园里的兄弟写的,但拷下来总是不好用,不过知道了要用到soapsdk3.0,于是down了一个,安装完了就是一堆com,又开始google"mssoap30.dll"+"vc",这次搜索所有网页,出来了一堆英文页面,翻了好几页,看了一大堆的英文后,终于找到了点眉目,也稍微整理一下。
1:先安装soapsdk3.0(http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe)
2:当然就是写代码
WSWrapper.h
1#ifndef _WS_WRAPPER_H_
2#define _WS_WRAPPER_H_
3
4#import "msxml4.dll"
5#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
6 exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
7 "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
8#include <string>
9#include <Windows.h>
10
11using namespace MSXML2;
12using namespace MSSOAPLib30;
13using std::string;
14
15class WSWrapper
16{
17public:
18 WSWrapper(const char *wsURL,
19 const char *wsNameSapce,
20 const char *wsMethodName);
21 virtual ~WSWrapper();
22 string Hello(const string &strName);
23
24private:
25 const string _wsURL;
26 const string _wsNameSapce;
27 const string _wsMethodName;
28};
29
30
31#endif
2#define _WS_WRAPPER_H_
3
4#import "msxml4.dll"
5#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
6 exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
7 "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
8#include <string>
9#include <Windows.h>
10
11using namespace MSXML2;
12using namespace MSSOAPLib30;
13using std::string;
14
15class WSWrapper
16{
17public:
18 WSWrapper(const char *wsURL,
19 const char *wsNameSapce,
20 const char *wsMethodName);
21 virtual ~WSWrapper();
22 string Hello(const string &strName);
23
24private:
25 const string _wsURL;
26 const string _wsNameSapce;
27 const string _wsMethodName;
28};
29
30
31#endif
WSWrapper.cpp
1#include "WSWrapper.h"
2
3WSWrapper::WSWrapper(const char *wsURL,
4 const char *wsNameSapce,
5 const char *wsMethodName)
6 : _wsURL(wsURL),
7 _wsNameSapce(wsNameSapce),
8 _wsMethodName(wsMethodName)
9{
10
11}
12
13WSWrapper::~WSWrapper()
14{
15
16}
17
18string WSWrapper::Hello(const string &strName)
19{
20 try
21 {
22 HRESULT hr = CoInitialize(NULL);//初始化com环境
23 if(FAILED(hr))
24 {
25 //出错了
26 }
27
28 ISoapSerializerPtr Serializer;
29 ISoapReaderPtr Reader;
30 ISoapConnectorPtr Connector;
31
32 //连接到WebService
33 hr = Connector.CreateInstance(__uuidof(HttpConnector30));
34 if(FAILED(hr))
35 {
36 //创建com对象出错,一般是因为没有安装com
37 }
38
39 Connector->Property["EndPointURL"] = _wsURL.c_str();
40 Connector->Connect();
41 Connector->Property["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();
42
43 //开始创建webservice的请求Soap包
44 Connector->BeginMessage();
45 hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
46 if(FAILED(hr))
47 {
48 //创建com对象出错,一般是因为没有安装com
49 }
50 Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
51 Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");
52 Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
53 Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");
54
55 Serializer->StartBody(L"NONE");
56 Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
57 Serializer->StartElement(L"strName", "", "NONE", "");
58 Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
59 Serializer->WriteString(strName.c_str());
60 Serializer->EndElement();
61 Serializer->EndElement();
62 Serializer->EndBody();
63 Serializer->EndEnvelope();
64
65 Connector->EndMessage();
66
67 //解析返回的soap包
68 hr = Reader.CreateInstance(__uuidof(SoapReader30));
69 if(FAILED(hr))
70 {
71 //创建com对象出错,一般是因为没有安装com
72 }
73 Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
74 string strResult((const char*)Reader->RpcResult->text);
75
76 return strResult;
77
78
79 }
80 catch()
81 {
82 //got a exception
83 }
84 return "error";
85}
86
87
2
3WSWrapper::WSWrapper(const char *wsURL,
4 const char *wsNameSapce,
5 const char *wsMethodName)
6 : _wsURL(wsURL),
7 _wsNameSapce(wsNameSapce),
8 _wsMethodName(wsMethodName)
9{
10
11}
12
13WSWrapper::~WSWrapper()
14{
15
16}
17
18string WSWrapper::Hello(const string &strName)
19{
20 try
21 {
22 HRESULT hr = CoInitialize(NULL);//初始化com环境
23 if(FAILED(hr))
24 {
25 //出错了
26 }
27
28 ISoapSerializerPtr Serializer;
29 ISoapReaderPtr Reader;
30 ISoapConnectorPtr Connector;
31
32 //连接到WebService
33 hr = Connector.CreateInstance(__uuidof(HttpConnector30));
34 if(FAILED(hr))
35 {
36 //创建com对象出错,一般是因为没有安装com
37 }
38
39 Connector->Property["EndPointURL"] = _wsURL.c_str();
40 Connector->Connect();
41 Connector->Property["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();
42
43 //开始创建webservice的请求Soap包
44 Connector->BeginMessage();
45 hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
46 if(FAILED(hr))
47 {
48 //创建com对象出错,一般是因为没有安装com
49 }
50 Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
51 Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");
52 Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
53 Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");
54
55 Serializer->StartBody(L"NONE");
56 Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
57 Serializer->StartElement(L"strName", "", "NONE", "");
58 Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
59 Serializer->WriteString(strName.c_str());
60 Serializer->EndElement();
61 Serializer->EndElement();
62 Serializer->EndBody();
63 Serializer->EndEnvelope();
64
65 Connector->EndMessage();
66
67 //解析返回的soap包
68 hr = Reader.CreateInstance(__uuidof(SoapReader30));
69 if(FAILED(hr))
70 {
71 //创建com对象出错,一般是因为没有安装com
72 }
73 Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
74 string strResult((const char*)Reader->RpcResult->text);
75
76 return strResult;
77
78
79 }
80 catch()
81 {
82 //got a exception
83 }
84 return "error";
85}
86
87
TestApp.cpp
1#include "WSWrapper.h"
2
3int main()
4{
5
6 WSWrapper wsWrapper("http://localhost/TestWebService/Service1.asmx",
7 "http://localhost/TestWebService/",
8 "Hello");
9
10 string strName = "Lucy";
11 string strResult = wsWrapper.Hello(strName);
12
13 printf("%s", strResult.c_str());
14
15 getchar();
16
17 return 0;
18}
19
2
3int main()
4{
5
6 WSWrapper wsWrapper("http://localhost/TestWebService/Service1.asmx",
7 "http://localhost/TestWebService/",
8 "Hello");
9
10 string strName = "Lucy";
11 string strResult = wsWrapper.Hello(strName);
12
13 printf("%s", strResult.c_str());
14
15 getchar();
16
17 return 0;
18}
19
C#写的一个测试WebService
1using System.ComponentModel;
2using System.Web.Services;
3
4namespace TestWebService
5{
6 /// <summary>
7 /// Service1 的摘要说明。
8 /// </summary>
9 [WebService(Namespace="http://localhost/TestWebService/")]
10 [System.Web.Services.Protocols.SoapRpcService]
11 public class Service1 : System.Web.Services.WebService
12 {
13 public Service1()
14 {
15 //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
16 InitializeComponent();
17 }
18
19 组件设计器生成的代码
45
46 // WEB 服务示例
47 // HelloWorld() 示例服务返回字符串 Hello World
48 // 若要生成,请取消注释下列行,然后保存并生成项目
49 // 若要测试此 Web 服务,请按 F5 键
50
51 [WebMethod]
52 public string Hello(string strName)
53 {
54 return "Hello, " + strName;
55 }
56 }
57}
58
2using System.Web.Services;
3
4namespace TestWebService
5{
6 /// <summary>
7 /// Service1 的摘要说明。
8 /// </summary>
9 [WebService(Namespace="http://localhost/TestWebService/")]
10 [System.Web.Services.Protocols.SoapRpcService]
11 public class Service1 : System.Web.Services.WebService
12 {
13 public Service1()
14 {
15 //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
16 InitializeComponent();
17 }
18
19 组件设计器生成的代码
45
46 // WEB 服务示例
47 // HelloWorld() 示例服务返回字符串 Hello World
48 // 若要生成,请取消注释下列行,然后保存并生成项目
49 // 若要测试此 Web 服务,请按 F5 键
50
51 [WebMethod]
52 public string Hello(string strName)
53 {
54 return "Hello, " + strName;
55 }
56 }
57}
58