我正在尝试从代码中执行 SOAP 请求。所以我使用 SOAP UI 来测试 Web 服务,有必要通过一个 certificate.pem,运行请求后它的工作如下。
我决定使用来自 SOAP UI 的相同 xml 请求的请求库来测试 soap 请求,如您在以下代码中所见:
import requests
from requests import Session
from requests.auth import HTTPBasicAuth
cert_file="path to cert"
key_file="path to key"
endpoint='https://cifinpruebas.asobancaria.com/InformacionComercialWS/services/InformacionComercial?wsdl'
session = Session()
session.auth = HTTPBasicAuth("user", "password")
body = """<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inf="http://infocomercial.cifin.asobancaria.com">
<soapenv:Header/>
<soapenv:Body>
<inf:consultaXml soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<in0 xsi:type="dto:ParametrosConsultaDTO" xmlns:dto="http://dto.infocomercial.cifin.asobancaria.com">
<codigoInformacion xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">1401</codigoInformacion>
<motivoConsulta xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">24</motivoConsulta>
<numeroIdentificacion xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">80775779</numeroIdentificacion>
<tipoIdentificacion xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">1</tipoIdentificacion>
</in0>
</inf:consultaXml>
</soapenv:Body>
</soapenv:Envelope>"""
body = body.encode('utf-8')
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body))})
response = session.post(url=endpoint, data=body, cert=(cert_file, key_file))
print(response)
我正在使用 RSA 私钥和证书,但在响应中我收到以下错误:
<Response [500]>
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode
xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction
</faultcode>
<faultstring>no SOAPAction header!</faultstring>
<detail>
<ns2:hostname
xmlns:ns2="http://xml.apache.org/axis/">ttuweb3
</ns2:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
所以这是一个服务器错误(错误 500),但我很困惑,因为我说请求正在使用 SOAP UI 而不是在 python 中工作。有什么解释吗?
注意: 我也一直在使用 Zeep 库,但结果完全一样。
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='ttuweb3.universo.corp', port=443): Max retries exceeded with url: /InformacionComercialWS/services/InformacionComercial (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f993b793c40>: Failed to establish a new connection: [Errno -2] Name or service not known'))
编辑 1:更新标题 我添加了以下代码,解决了之前的错误。 session.headers.update({"SOAPAction": ""})
但是出现了新的错误。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.generalException</faultcode>
<faultstring>WSDoAllReceiver: Request does not contain required Security header</faultstring>
<detail>
<ns1:hostname
xmlns:ns1="http://xml.apache.org/axis/">ttuweb3
</ns1:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
您是否尝试拦截两个请求以比较它们发送的内容? SoapUI 可能包含乍看之下不可见的行。尝试使用 HTTP 工具包。