WSDL 绑定:SOAP风格、编码与动作
逐项核对name、type、style、transport、操作和消息体设置。
学完这一章,你能够
- 逐项核对name、type、style、transport、操作和消息体设置。
- 说明当前文件的版本与抽象/具体层次
- 核对真实引用及实际调用,不混同检查范围
绑定连接什么
binding的name是自身名称,type是所绑定的portType QName,不是某个TCP端口数字。soap:binding的style常为document或rpc,transport给出传输绑定标识;二者与实际服务地址不同。
rpc描述按过程调用风格组织消息,document描述文档风格。style与soap:body的use不是同一维度,不能把rpc必然等同于encoded或把document/literal当成一个可任意省略的魔法值。
操作及消息体
binding中的operation必须指向相应抽象操作,不能遗漏其name后仍把片段当完整有效示例。soap:operation给出SOAPAction等绑定信息;input/output中的soap:body规定literal或encoded等使用方式。
本工程用document与literal,三个操作分别绑定对应的动作URI。literal依赖消息声明的XML结构,不要求强行加入SOAP encodingStyle。WSDL中的soap扩展命名空间与信封命名空间不同,不能互换。
地址和运行
service中的port引用binding,soap:address给出地址。示例模板为回环8859端口,自动测试通过location显式覆盖为随机端口,并记录该实际地址。地址覆盖不修改抽象类型,服务器响应仍须满足同一契约。
完整绑定字段
| 字段 | 值类别或本站值 | 作用与限制 |
|---|---|---|
| binding@name | SiteBinding | 绑定自身名称 |
| binding@type | tns:SitePortType | QName指向抽象接口,非数字端口 |
| soap:binding@style | document或rpc | 消息风格,非use同义词 |
| soap:binding@transport | http://schemas.xmlsoap.org/soap/http | 传输绑定标识,非服务端地址 |
| operation@name | getName、getUrl、getLesson | 对应抽象操作,不能漏写 |
| soap:operation@soapAction | 操作URI | 按具体协议版本和绑定使用 |
| soap:body@use | literal或encoded | 本例literal,不强行使用encodingStyle |
| port@binding | tns:SiteBinding | 引用绑定 |
| soap:address@location | 回环URL | 模板和实际覆盖地址分开记录 |
style与use分开
| 维度 | 选择 | 本工程 |
|---|---|---|
| style | document / rpc | document |
| use | literal / encoded | literal |
| transport | 具体协议绑定 | SOAP 1.1 over HTTP |
| address | 实际服务URL | 回环端点,非公网部署 |
本章独立核心源码与验证步骤
from pathlib import Path
from lxml import etree
B=Path(__file__).resolve().parent
root=etree.parse(str(B/'public/service.wsdl'),etree.XMLParser(resolve_entities=False,no_network=True)).getroot()
N={'w':'http://schemas.xmlsoap.org/wsdl/','s':'http://schemas.xmlsoap.org/wsdl/soap/'}
binding=root.find('w:binding',N);assert binding.get('type')=='tns:SitePortType'
soap=binding.find('s:binding',N);assert soap.get('style')=='document';assert soap.get('transport')=='http://schemas.xmlsoap.org/soap/http'
operations={op.get('name'):op.find('s:operation',N).get('soapAction') for op in binding.findall('w:operation',N)}
assert operations=={'getName':'urn:mbsky:site#getName','getUrl':'urn:mbsky:site#getUrl','getLesson':'urn:mbsky:site#getLesson'}
assert all(body.get('use')=='literal' for body in binding.xpath('.//s:body',namespaces=N))
assert root.find('w:service/w:port',N).get('binding')=='tns:SiteBinding'
print({'style':'document','use':'literal','operations':operations,'location':root.find('w:service/w:port/s:address',N).get('location')})
所需环境:Python 3.12 lxml;PHP 8.4.27 SOAP;回环HTTP
当前源码 SHA256:3169b3d1a530d864f4ef086e6c7653f6ab35dcf178b2df9d49a12814e9041af4
- 本章固定结构、引用或目录查找断言成立
- 声明为抽象或本地目录的内容不冒充协议实现
常见错误与边界
- 把portType当作具体端点port。
- 把早期1.2语法与1.1/2.0混写。
- 把本地JSON目录当成UDDI实现。
练习与解答
给getLesson增加一个受控输入,保持XSD、消息和适配类一致,再真实调用并记录结果。
展开参考解答
先更新并检查契约引用,再更新响应包装及客户端;不要用显示一段JSON替代实际SOAP调用。