前几天试了下在postman上用http post方式调用webservice接口,结果发现也是可以的,然后就想到了在Python中用requests是否可行,于是就有了这篇笔记。
1、headers设置
headers = {
'Content-Type': "text/xml",
'SOAPAction': "application/soap+xml;charset=utf-8"
}
2、数据准备,发送请求,由于用到的是项目地址,屏蔽下;
请求报文记得用<!CDATA[]]> 包起来
url = ""
payload = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " \
"xmlns:zj=\"http://**.****.****.com\">" \
"<soapenv:Header/>" \
"<soapenv:Body> " \
"<zj:kHZX4OAXml>" \
"<![CDATA[你的xml请求数据]]>" \
"</zj:kHZX4OAXml> " \
"</soapenv:Body>" \
"</soapenv:Envelope>"
response = requests.request("POST", url, data=data, headers=headers)
3、数据解析
| 1)将响应结果字符串转换成xml对象 |

本文记录了使用Python通过requests库调用Webservice接口的步骤,包括headers设置、XML数据的发送与解析。遇到返回结果为bytes类型时,通过decode()进行解码。同时,提到了利用suds库简化Webservice接口调用的方法,详细内容将在下篇笔记中分享。
最低0.47元/天 解锁文章
511

被折叠的 条评论
为什么被折叠?



