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

SOAP请求在SOAPUI正常但Python Requests调用返回500错误求助

问题:Python Requests发送带Bearer Token的SOAP请求返回500错误,SOAPUI可正常执行
  • 此前无Bearer Token的SOAP请求在Python和SOAPUI中均正常运行,但添加Token后,Python调用返回<Response [500]>,错误信息为No es posible finalizar la operación, favor intentar nuevamente
  • 已确认Bearer Token、请求内容与SOAPUI完全一致,后续添加wsa相关Header后仍报同类500错误
  • SOAPUI与Python的请求头差异仅为User-Agent:SOAPUI使用Apache-HttpClient/4.5.5,Python默认使用python-requests/2.30.0

排查与解决步骤

1. 完全复刻SOAPUI的请求头

很多SOAP服务对请求头细节极其敏感,必须1:1复制SOAPUI的所有请求头,尤其不要遗漏SOAPAction(绝大多数SOAP服务依赖该字段):

import requests

url = "https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc"
bearer_token = "你的Bearer Token内容"
soap_request_body = """这里粘贴SOAPUI里的完整请求体"""

# 完全照搬SOAPUI的请求头
headers = {
    "accept-encoding": "gzip,deflate",
    "authorization": f"Bearer {bearer_token}",
    "connection": "Keep-Alive",
    "content-type": "text/xml;charset=UTF-8",
    "host": "externalchannels-pro-ws.gruposancorseguros.com",
    "user-agent": "Apache-HttpClient/4.5.5",  # 强制和SOAPUA一致
    "SOAPAction": "http://对应的SOAP命名空间/目标方法名"  # 补上SOAPUI中存在的SOAPAction头
}

response = requests.post(url, data=soap_request_body, headers=headers)
print(response.status_code)
print(response.text)

2. 检查请求体的编码与格式

  • 必须用data=参数传递SOAP文本,不要用json=;确保请求体编码为UTF-8,和SOAPUI一致
  • 逐行对比请求体的标签、命名空间、大小写,SOAP对这些细节零容忍

3. 调整Requests的请求参数

部分服务对连接方式敏感,尝试添加以下参数测试:

# 禁用自动重定向,关闭SSL验证(仅测试用,生产环境请勿关闭验证)
response = requests.post(
    url,
    data=soap_request_body,
    headers=headers,
    allow_redirects=False,
    verify=False
)

4. 捕获完整请求日志对比

打印Python发送的完整请求详情,和SOAPUI的请求逐字段对比:

import logging
import http.client

# 开启Requests的调试日志
http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# 发送请求,控制台会输出完整的请求头、请求体
response = requests.post(url, data=soap_request_body, headers=headers)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:32:36