【发布时间】:2019-07-25 09:47:44
【问题描述】:
我知道 c# 和 Python 是两种不同的语言,但是我认为 xml soap 代码应该大致相似,但我看不到让它工作。在 c# 中工作的 url 最终只是获取了我的网页代码,我正在使用 POST 并且想要输入身份验证和输入以在服务器中检索信息。我该怎么做呢
我尝试了各种 url,各种请求格式,但是我不太熟悉 python 和 soap 的交互方式,所以我所能做的就是在线搜索并调整现有代码,但它们似乎都在连接时发出错误(我认为这是一个 url 问题)或检索网页本身的代码。
import requests
from xml.etree import ElementTree as ET
url= "http://website/websiteAPI.asmx?op=GetInformation" headers = {
'Content-Type' : 'application/soap+xml' 'charset=utf-8'
'SOAPAction' "http://website/websiteApi/GetInformation"}
body = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAssetListByType
xmlns="http://website/websiteApi/GetInformation">
<user>test</user>
<pass>456</pass>
<RanCode>ver</RanCode>
</GetAssetListByType> </soap:Body> </soap:Envelope>"""
response = requests.post(url, data=body,headers=headers)
print (response.content)
print (response.status_code)
我希望登录并能够从服务器检索输出,但是我收到了错误 500/404/301 或网页代码。
【问题讨论】:
-
你为什么
from xml.etree import ElementTree as ET? -
http 有标头,c# 中的默认标头可能与 xml soap 和 python 不同。要找到问题的根本原因,您需要使用像 wireshark 或 fiddler 这样的嗅探器并比较工作代码和非工作代码。修复使非工作 http 标头看起来与工作 http 标头完全相同。