SOAP 实例

下面给你一个完整、可直接运行、真实项目中最常见的 SOAP 实例,从 WSDL → 请求 → 响应 → 错误,一步一步全都有。

场景:查询天气(经典的公开 SOAP 服务)

真实可用地址(全球天气服务,已运行 15+ 年):
https://www.w3schools.com/xml/tempconvert.asmx

1. WSDL 地址(定义了所有接口)

https://www.w3schools.com/xml/tempconvert.asmx?WSDL

里面只有两个方法:

  • CelsiusToFahrenheit
  • FahrenheitToCelsius

2. 完整 SOAP 1.1 请求实例(摄氏度 → 华氏度)

HTTP 请求(可以用 Postman / curl / 代码直接发)

POST /xml/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
SOAPAction: "https://www.w3schools.com/xml/CelsiusToFahrenheit"
Content-Length: 352

<?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>
    <CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/">
      <Celsius>30</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>

正常响应(HTTP 200)

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

<?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>
    <CelsiusToFahrenheitResponse xmlns="https://www.w3schools.com/xml/">
      <CelsiusToFahrenheitResult>86</CelsiusToFahrenheitResult>
    </CelsiusToFahrenheitResponse>
  </soap:Body>
</soap:Envelope>

3. 故意发错请求 → 触发 SOAP Fault(HTTP 500)

把参数名写错(Celsius → Celcius):

<Celcius>30</Celcius>   <!-- 拼写错误 -->

服务器返回的完整错误(500 + Fault)

HTTP/1.1 500 Internal Server Error
Content-Type: text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Client</faultcode>
      <faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: https://www.w3schools.com/xml/CelsiusToFahrenheit...
      <!-- 实际这里会提示参数找不到或类型错误 --></faultstring>
      <detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

4. 用 curl 一键测试(复制粘贴就能跑)

curl -X POST https://www.w3schools.com/xml/tempconvert.asmx \
  -H "Content-Type: text/xml; charset=utf-8" \
  -H "SOAPAction: \"https://www.w3schools.com/xml/CelsiusToFahrenheit\"" \
  -d '<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>
    <CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/">
      <Celsius>25</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>' -s | xmllint --format -

输出(77°F):

<CelsiusToFahrenheitResult>77</CelsiusToFahrenheitResult>

5. 国内真实项目中最常见的完整实例(银行类)

<!-- 请求:查询账户余额 -->
POST /ebank/services/AccountService HTTP/1.1
Host: 10.10.20.30
Content-Type: text/xml; charset=gb2312
SOAPAction: ""

<?xml version="1.0" encoding="gb2312"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <queryBalance xmlns="http://www.chinabank.com/ws">
      <acctNo>6222021234567890</acctNo>
      <certType>01</certType>
      <certNo>110101199001011234</certNo>
    </queryBalance>
  </soap:Body>
</soap:Envelope>

响应:

<queryBalanceResponse xmlns="http://www.chinabank.com/ws">
  <balance>58392.17</balance>
  <currency>CNY</currency>
</queryBalanceResponse>

错误响应(余额不足):

<soap:Fault>
  <faultcode>soap:Client</faultcode>
  <faultstring>BIZ_1001:余额不足</faultstring>
</soap:Fault>

现在你已经拥有了一个从零到一、可以直接复制运行的完整 SOAP 实例,包括正常请求、成功响应、错误响应、curl 测试命令,完全符合真实生产环境。

需要我再给你生成 Java、Python、C#、Postman 集合、或你自己公司某个接口的具体 SOAP 报文,直接把 WSDL 或接口文档发我即可!

文章已创建 2965

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部