随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

vc6控制台程序利用SoapToolkit3.0调用WebService

vc6控制台程序利用SoapToolkit3.0调用WebService

1. 首先要安装SoapToolkit3.0安装包并安装(我的安装目录为:D:\Program Files\MSSOAP\)

2. 新建vc控制台程序(空项目),项目名称:WinConsole6InvokeWebService,添加一个c++源文件(main.cpp),将SOAP安装目录下的lib文件D:\Program Files\MSSOAP\Lib\mssoap30.lib复制到项目文件夹下。

3。添加源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <iostream>
#include <vector>
 
#import "msxml4.dll"
 
using namespace std;
using namespace MSXML2;
 
#import "C:\Program Files\Common Files\MSSoap\Binaries\MSSOAP30.dll" \
        exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
        "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
 
using namespace MSSOAPLib30;
 
void query(char* EndPointURL, char* Namespace, char* method, vector<string>& v)
{
    ISoapSerializerPtr Serializer;
    ISoapReaderPtr Reader;
    ISoapConnectorPtr Connector;
     
    // Connect to the service
    Connector.CreateInstance(__uuidof(HttpConnector30));
    Connector->Property["EndPointURL"] = EndPointURL;        // 接口位置
    Connector->Connect();                                    // 和服务器连接
     
    // Begin message
    Connector->Property["SoapAction"] = _bstr_t(Namespace) + _bstr_t(method);
    Connector->BeginMessage();
 
    Serializer.CreateInstance(__uuidof(SoapSerializer30));
 
    // 将serializer连接到connector的输入字符串
    Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
 
    // 创建SOAP消息
    Serializer->StartEnvelope("soap", "", "");
    Serializer->StartBody("body");
    Serializer->StartElement(method, Namespace, "", ""); // 命名空间必须有
 
    for(vector<string>::iterator it = v.begin(); it != v.end(); it++)
    {
        Serializer->StartElement("username", Namespace, "", "");
        Serializer->WriteString(it->c_str());
        Serializer->EndElement();
    }
 
    Serializer->EndElement();
 
    Serializer->EndBody();
    Serializer->EndEnvelope();
 
    Connector->EndMessage();             // Send the message to the web service
 
    // 读取响应
    Reader.CreateInstance(__uuidof(SoapReader30));
    Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
 
    printf("Answer: %s\n", (const char*)Reader->RpcResult->text); // Reader->RpcResult->Gettext()等效
}
 
int main(int argc, char* argv[])
{
    CoInitialize(NULL);
    char* EndPointURL = "http://192.168.0.100/WebService1/Service.asmx";
    char* Namespace = "http://tempuri.org/";
 
    vector<string> v1, v2;
 
    v2.push_back("JoeBlack");
    query(EndPointURL, Namespace, "Hello", v2);
 
    CoUninitialize();
    getchar();
 
    return 0;
}

这样,程序就完成了,运行起来就可以得到WebService的服务了。

其中的WebService服务是用ASP.NET2005(C#)开发的,源码如下:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
 
        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
 
    [WebMethod(Description = "Let's say \"Hi\"")]
    public string Hi()
    {
        return "Hello World, Happy New Year!";
    }
 
    [WebMethod(Description = "Hello JoeBlack")]
    public string Hello(string username)
    {
        return username + ", Happy New Year!";
    }
 
    [WebMethod(Description = "求和的方法")]
    public double addition(double i, double j)
    {
        return i + j;
    }
 
    [WebMethod(Description = "求差的方法")]
    public double subtract(double i, double j)
    {
        return i - j;
    }
 
    [WebMethod(Description = "求积的方法")]
    public double multiply(double i, double j)
    {
        return i * j;
    }
 
    [WebMethod(Description = "求商的方法")]
    public double division(double i, double j)
    {
        if (j != 0)
            return i / j;
        else
            return 0;
    }
}

调用的Hello方法,其调用方式如下:

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebService1/Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Hello"

<?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>
    <Hello xmlns="http://tempuri.org/">
      <username>string</username>
    </Hello>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <HelloResponse xmlns="http://tempuri.org/">
      <HelloResult>string</HelloResult>
    </HelloResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebService1/Service.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <Hello xmlns="http://tempuri.org/">
      <username>string</username>
    </Hello>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <HelloResponse xmlns="http://tempuri.org/">
      <HelloResult>string</HelloResult>
    </HelloResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP POST

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebService1/Service.asmx/Hello HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
posted on   清清飞扬  阅读(3214)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
< 2011年2月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 1 2 3 4 5
6 7 8 9 10 11 12

点击右上角即可分享
微信分享提示