抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

导语

最近项目用到处理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

评论吧



本站总访问量为 访客数为

鲁 ICP 备 20018157 号-1
Copyright 2021 - 2022 sizaif. All Rights Reserved