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
相关产品推荐
相关产品推荐

