PAYATHON 2026

从 WSDL 解析可用的 SOAP 操作

数据小陈

WWSAPI 可以做,不过它的工作方式和 gSOAP 不同。gSOAP 会在运行时反射 WSDL 并生成代码,WWSAPI 不这么做。WWSAPI 运行时没有一组稳定公开的高层 API,能直接返回 WSDL 里的全部 operation 和参数类型。常见做法是用 WsUtil.exe 在编译期根据 WSDL/XSD 生成 C 代理和序列化代码。如果需要在发送前动态加载 WSDL,通常得自己用 WWSAPI 的 XML Reader 遍历 WSDL,或者配合第三方 WSDL 解析器,先建立 operation/参数模型,再用 WWSAPI XML Writer 构造 SOAP Envelope。WsReadMetadata 可以读取 WSDL/Policy 元数据并拿到端点信息,但不能指望它直接给出完整的 operation 参数模型。

实现路径

  1. 服务固定时,优先用 WsUtil.exe。用 WSDL/XSD 生成 C 代理、消息结构和 operation 描述,调用时使用 WsCreateServiceProxy、WsCall 等 API。这是 WWSAPI 最稳妥的用法。
  2. 需要在运行时动态处理时,用 WsCreateReader / WsSetInput / WsReadNode / WsGetReaderNode 遍历 WSDL XML,识别 wsdl:portType、wsdl:operation、wsdl:message、wsdl:part、xsd:element、xsd:complexType 等节点,记录方法名、soapAction、参数名和类型 QName 字符串。
  3. 构造 Envelope 时,用 WsCreateWriter / WsWriteStartElement / WsWriteText / WsWriteEndElement 写出 SOAP XML;也可以先拼成 XML 字符串,再交给发送层。发送层可以用 WWSAPI 的 channel/message API,或按项目约束使用 WinHTTP。

WSDL 解析要点

  • WSDL 1.1 中,portType 下的 operation 给出逻辑操作;binding 下的 operation 给出 soapAction 和 body 样式;message / part 指向 element 或 type。
  • document/literal 与 rpc/literal 的 Body 结构不同。document/literal 通常把 WSDL 中引用的全局元素作为 soap:Body 的子元素;rpc/literal 常用操作名作为包装元素。
  • 还需要处理 wsdl:import、xsd:import、xsd:include 和命名空间前缀。只保存类型名字符串时,建议统一存成 {namespace}localName 形式。
  • 复杂类型、数组、choice、nillable、minOccurs/maxOccurs 等内容,仅靠简单遍历很难完整还原;如果 WSDL 复杂,建议使用成熟的 WSDL/XSD 解析库,而不是手写完整解析器。

C++ 示意代码

下面的代码只演示 WWSAPI XML Reader/Writer 的组织方式,错误处理、writer 输出设置和 WSDL import 解析都省略了。

#include <windows.h>
#include <webservices.h>
#include <string>
#include <vector>

#pragma comment(lib, "WebServices.lib")

struct ParamInfo {
    std::string name;
    std::string typeQName;   // 例如 {http://example.com}Add
};

struct OperationInfo {
    std::string name;
    std::string soapAction;
    std::vector<ParamInfo> inputParams;
};

static WS_XML_STRING XmlStr(const char* s) {
    WS_XML_STRING x;
    x.length = (ULONG)strlen(s);
    x.bytes = (BYTE*)s;
    return x;
}

// 仅演示遍历节点。真实实现要识别 WSDL/XSD 节点、处理 import 和命名空间。
void ScanWsdl(WS_XML_READER* reader, std::vector<OperationInfo>& ops) {
    WS_ERROR* error = nullptr;
    for (;;) {
        HRESULT hr = WsReadNode(reader, error);
        if (FAILED(hr)) break; // 实际应区分 EOF 和错误

        const WS_XML_NODE* node = nullptr;
        hr = WsGetReaderNode(reader, &node, error);
        if (FAILED(hr) || node == nullptr) continue;
        if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) continue;

        const WS_XML_ELEMENT_NODE* e = (const WS_XML_ELEMENT_NODE*)node;
        // 比较 e->localName 和 e->namespaceUri,
        // 识别 wsdl:operation、wsdl:message、wsdl:part、xsd:element 等。
    }
}

HRESULT BuildEnvelope(WS_XML_WRITER* writer,
                      const OperationInfo& op,
                      const std::vector<std::pair<std::string, std::string>>& args,
                      WS_ERROR* error) {
    WS_XML_STRING soapPrefix = XmlStr("soap");
    WS_XML_STRING soapNs = XmlStr("http://schemas.xmlsoap.org/soap/envelope/");
    WS_XML_STRING env = XmlStr("Envelope");
    WS_XML_STRING body = XmlStr("Body");
    WS_XML_STRING tnsPrefix = XmlStr("tns");
    WS_XML_STRING tns = XmlStr("urn:example"); // 应使用 WSDL 的 targetNamespace
    WS_XML_STRING opName = XmlStr(op.name.c_str());

    HRESULT hr = WsWriteStartElement(writer, &soapPrefix, &env, &soapNs, error);
    if (FAILED(hr)) return hr;
    hr = WsWriteStartElement(writer, &soapPrefix, &body, &soapNs, error);
    if (FAILED(hr)) return hr;
    hr = WsWriteStartElement(writer, &tnsPrefix, &opName, &tns, error);
    if (FAILED(hr)) return hr;

    for (const auto& a : args) {
        WS_XML_STRING p = XmlStr(a.first.c_str());
        WS_XML_STRING v = XmlStr(a.second.c_str());
        hr = WsWriteStartElement(writer, &tnsPrefix, &p, &tns, error);
        if (FAILED(hr)) return hr;
        hr = WsWriteText(writer, &v, error);
        if (FAILED(hr)) return hr;
        hr = WsWriteEndElement(writer, error);
        if (FAILED(hr)) return hr;
    }

    WsWriteEndElement(writer, error); // operation
    WsWriteEndElement(writer, error); // Body
    WsWriteEndElement(writer, error); // Envelope
    return WsFlushWriter(writer, error);
}

注意事项

  • WS_XML_STRING 不会复制字符串内容,bytes 指向的内存必须在调用期间有效。动态字符串不要指向已经析构的局部 std::string。
  • SOAP 1.1 和 SOAP 1.2 的 Envelope 命名空间不同,必须按 WSDL/binding 选择。
  • 构造 Body 子元素时,元素名来自 WSDL 的 element QName 或 rpc 包装规则,不能一律用 operation 名。
  • WWSAPI 的代理和消息描述通常依赖编译期生成的元数据;如果要做完全动态调用,发送层以及消息头/SOAPAction 都需要额外处理,建议先用抓包工具核对请求格式。
  • 如果 WSDL 很复杂,优先考虑用 WsUtil.exe 生成代码,或者用成熟的 WSDL/XSD 解析库配合 WWSAPI 构造和发送消息。

备注:内容仅供参考。