python 处理soap-post方法

18 篇文章 3 订阅
10 篇文章 1 订阅
本文介绍了在处理SOAP接口时遇到的返回结果为二进制流并导致乱码的难题。作者通过分析,确定问题在于返回结果的解码。通过在HTTP请求头中添加'Accept-Encoding':'utf-8',成功解决了乱码问题。博客内容涉及到Python的requests库和SOAP协议的使用。
摘要由CSDN通过智能技术生成

导语

最近用到处理soap接口协议,使用post方法发送数据

在这个地方停留许久,故将解决方法记录下来

soap

soap 依赖于XML文件

首先要处理XML文件

python中有xmltodict,dicttoxml两个库可以使用

使用方法

两种解决方法:

一种是 requests方法

另一种是使用suds

两种方法可参考:

Call a SOAP Service with plain old requests

Calling a SOAP WebService with Python Suds

# Call a SOAP Service with plain old requests
import requests
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?xml version="1.0" encoding="UTF-8"?>
			 <SOAP-ENV:Envelope xmlns:ns0="http://ws.cdyne.com/WeatherWS/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" 
			 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
			 	<SOAP-ENV:Header/>
			 	  <ns1:Body><ns0:GetWeatherInformation/></ns1:Body>
			 </SOAP-ENV:Envelope>"""

response = requests.post(url,data=body,headers=headers)
print response.content

---------------------华丽的分割线1----------------------

# Calling a SOAP WebService with Python Suds
from suds.client import Client
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
client = Client(url)
print client ## shows the details of this service

result = client.service.GetWeatherInformation() 
print result ## see: restult.txt below

这里采用的是requests方法

遇到问题

构造header``body,uri

无论是使用python代码还是使用postman工具

获得返回的结果都是二进制流,解析后乱码

如下图:

image-20211125121849081

尝试各种设置编码为'utf-8' 仍输出乱码, 初步判断: 对返回的结果解析编码的问题

构造的body为:

body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:exm=\"http://schemas.microsoft.com/exchange/services/2006/messages\" xmlns:ext=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n<soap:Header>\n<a:Action soap:mustUnderstand=\"1\">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>\n<a:To soap:mustUnderstand=\"1\">https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc</a:To>\n<a:ReplyTo>\n<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>\n</a:ReplyTo>\n</soap:Header>\n<soap:Body>\n<GetFederationInformationRequestMessage xmlns=\"http://schemas.microsoft.com/exchange/2010/Autodiscover\">\n<Request>\n<Domain>" + domain + "</Domain>\n</Request>\n</GetFederationInformationRequestMessage>\n</soap:Body>\n</soap:Envelope>"

构造的header为

headers = {
    'content-type': 'text/xml',
    'SOAPAction': 'http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation',
    'User-Agent': 'AutodiscoverClient',
}

解决方法:

判断为返回结果解码造成的

尝试在header中添加'Accept-Encoding':'utf-8'

如下:

headers = {
    'content-type': 'text/xml',
    'SOAPAction': 'http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation',
    'User-Agent': 'AutodiscoverClient',
    'Accept-Encoding':'utf-8',
}

结果:

乱码问题成功解决:

image-20211125122254951

要使用 Python 中的 `requests` 库构造 SOAP 请求,你需要创建一个包含 SOAP 消息的字符串,并将其作为请求的正文发送。下面是一个示例: ```python import requests # 构造 SOAP 请求消息 soap_message = ''' <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:example="http://www.example.com/"> <soap:Header/> <soap:Body> <example:SomeRequest> <example:Parameter1>Value1</example:Parameter1> <example:Parameter2>Value2</example:Parameter2> </example:SomeRequest> </soap:Body> </soap:Envelope> ''' # 设置请求头和请求正文 headers = { 'Content-Type': 'application/soap+xml', 'charset': 'UTF-8', 'SOAPAction': 'http://www.example.com/SomeAction' } # 发送 SOAP 请求 response = requests.post('http://www.example.com/soap-endpoint', headers=headers, data=soap_message) # 处理响应 if response.status_code == 200: # 解析响应内容 response_data = response.content # 处理响应数据 # ... else: print('SOAP 请求失败:', response.status_code, response.reason) ``` 在上面的示例中,`soap_message` 变量包含了构造的 SOAP 请求消息。你需要根据你的具体 SOAP 消息格式进行修改。 然后,我们设置了请求头 `Content-Type` 为 `application/soap+xml`,并指定了 `SOAPAction` 的值。根据具体的 SOAP 服务,你可能需要调整这些值。 最后,我们使用 `requests.post` 方法发送了 SOAP 请求,并处理了响应。 请注意,这只是一个简单的示例,实际情况可能会更复杂。你可能需要根据具体的 SOAP 服务和消息格式进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值