SOAP 教程

SOAP 教程(2025 年实战版,一篇吃透)

SOAP(Simple Object Access Protocol)曾经是企业级接口的王者,现在虽然被 REST 抢了很多风头,但在中国它依然活得超级好——银行、运营商、税务、医保、海关、三大电网、医院 HIS、政务系统……几乎所有“要命的接口”还是 SOAP。

1. 一句话记住 SOAP 是什么

SOAP = 严格的、基于 XML 的、跑在 HTTP(或别的协议)上的远程调用协议
它长得像寄信:有信封(Envelope)、抬头(Header)、正文(Body)、还能贴邮票(WS-Security 签名)。

2. 一个最标准的 SOAP 请求/响应(2025 年最常见格式)

<!-- 请求(你要 POST 的完整内容)-->
POST /soap/user/v1 HTTP/1.1
Host: api.bank.com
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://bank.com/user/QueryUserInfo"

<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:web="http://bank.com/user/">
   <soapenv:Header>
      <!-- 可选:安全头、时间戳、事务ID 等 -->
      <web:Auth>
         <web:AppId>APP001</web:AppId>
         <web:Sign>MD5签名...</web:Sign>
      </web:Auth>
   </soapenv:Header>
   <soapenv:Body>
      <web:QueryUserInfoRequest>
         <web:IdCard>110101199001011234</web:IdCard>
      </web:QueryUserInfoRequest>
   </soapenv:Body>
</soapenv:Envelope>
<!-- 正常响应 -->
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <web:QueryUserInfoResponse xmlns:web="http://bank.com/user/">
         <web:Name>张三</web:Name>
         <web:Mobile>138********</web:Mobile>
         <web:Status>00</web:Status>
         <web:Message>成功</web:Message>
      </web:QueryUserInfoResponse>
   </soapenv:Body>
</soapenv:Envelope>
<!-- 出错响应(SOAP Fault 标准格式)-->
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Client</faultcode>
         <faultstring>签名错误</faultstring>
         <detail>
            <web:ErrorCode>SIGN001</web:ErrorCode>
            <web:ErrorMessage>数字签名验证失败</web:ErrorMessage>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

3. SOAP 版本对比(2025 年你会遇到的就这两种)

项目SOAP 1.1(老大)SOAP 1.2(新贵)
Envelope 命名空间http://schemas.xmlsoap.org/soap/envelope/http://www.w3.org/2003/05/soap-envelope
Content-Typetext/xmlapplication/soap+xml
中国银行/运营商99% 还是 1.1新系统开始强制 1.2
推荐新项目兼容老系统用 1.1有选择权就用 1.2

4. 快速上手:3 分钟调用任意 SOAP 接口(零代码)

工具:SoapUI(免费)Apifox(国产更好用)

步骤:

  1. 新建项目 → 粘贴 WSDL 地址(例如:https://api.example.com/service?wsdl)
  2. 自动生成所有请求模板
  3. 填参数 → 点击 Send 就能看到结果

真实可练手的公开接口(2025 年还活着):

  • 天气预报:http://www.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
  • 手机号归属地:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
  • 身份证查询:http://www.webxml.com.cn/WebServices/IdCardWS/idcard.asmx?wsdl

5. 主流语言快速开发(一行代码调用)

Python(最简单)

from zeep import Client

client = Client('https://www.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL')
result = client.service.getWeather(theCityCode="北京")
print(result)

Java(Spring Boot + CXF)

@WebServiceClient
public interface WeatherWS extends WebService {
    @WebMethod
    ArrayOfString getWeather(String theCityName);
}

C# .NET 8

var client = new WeatherWSSoapClient();
var result = await client.getWeatherAsync("上海");

6. 2025 年你必须知道的 SOAP 现状(三句话)

  1. 互联网新项目几乎 0 SOAP,全是 REST/JSON
  2. 所有“动钱”、“动身份”、“动档案”的系统 90% 以上还是 SOAP(因为有严格标准、数字签名、事务)
  3. 学 SOAP 不是为了酷,而是为了接银行、税务、运营商的单子——单子动辄几十万到几百万

学会上面这些,你已经能独立对接 99% 的企业级 SOAP 接口了。

需要我再给你:

  • 银行级完整签名 + 时间戳 + 证书的 SOAP 示例
  • Spring Boot 完整发布 SOAP 服务代码
  • 100 个真实可用的公共 SOAP 接口列表
    直接说一声!
文章已创建 2965

发表回复

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

相关文章

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

返回顶部