You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Country capitals SOAP API返回WSDL而非首都结果如何解决

Python调用SOAP接口异常原因及解决方案

核心问题点

  • 请求地址错误:当前使用的URL后缀带?WSDL,该地址是用于获取服务描述文档的端点,不是实际处理SOAP请求的服务端点,需删除URL末尾的?WSDL参数。
  • SOAP请求体语法错误:XML规范要求标签属性值必须用引号包裹,原请求体中定义soapenv和web命名空间时,对应的URI没有加双引号,导致服务端无法正确解析请求内容。

修正后完整代码

import requests
# 使用不带WSDL参数的实际服务端点
url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso"
headers = {'content-type': 'text/xml'}
# 修正XML命名空间的引号语法
body = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.oorsprong.org/websamples.countryinfo">
   <soapenv:Header/>
   <soapenv:Body>
      <web:CapitalCity>
         <web:sCountryISOCode>USA</web:sCountryISOCode>
      </web:CapitalCity>
   </soapenv:Body>
</soapenv:Envelope>"""
response = requests.post(url, data=body, headers=headers)
print(response.content)

内容的提问来源于stack exchange,提问作者Pn1040

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 00:18:03