我正在尝试使用 movildata.com 的 SOAP API 来获取车辆的最后位置,使用 IMEI 和 API key ,这是我的代码:
import requests
api_key = 'xxxxxxxxx'
imei = 'xxxxxxxxx'
request = """<?xml version = "1.0" encoding = "utf-8"?>
<soap12: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
<soap12: Body>
<getLastLocation xmlns = "http://ws.movildata.com/ws/wsUsers">
<apikey> {0} </ apikey>
<IMEI> {1} </ IMEI>
</ getLastLocation>
</ soap12: Body>
</ soap12: Envelope>
""".format(api_key, imei)
encoded_request = request.encode('utf-8')
headers = {"Host":"ws.movildata.com",
"Content-Type": "application/soap+xml; charset=utf-8",
"Content-Length":str(len(encoded_request))
}
response = requests.post(url='https://ws.movildata.com/wsUsers.asmx?op=getLastLocation',
headers=headers,
data=encoded_request
)
print(response.content)
print(response.status_code)
这将返回 XML 响应和 HTTP 状态代码 500
soap:ReceiverEl servidor no puede procesar la solicitud. ---> Un nombre no puede empezar con el car\xc3\xa1cter \' \', valor hexadecimal 0x20. L\xc3\xadnea 2, posici\xc3\xb3n 9.
我从西类牙语翻译过来,它的内容类似于
The server can not process the request. --- & gt; A name can not start with the character ' ';
我确保请求
中没有任何不必要的空白,但这似乎没有改变任何东西,你知道我可能做错了什么吗?
最佳答案
我发现您的 XML 格式有一些错误。 尝试按如下所述纠正它们。如果仍然出现错误,请在下面评论。
首先在 <soap12:
中的命名空间声明中元素, xmlns
之间不能有空格和xsi
.
全部改正:xmlns:xsi
, xmlns:xsd
和xmlns:soap12
在 <getLastLocation
,您刚刚拥有xmlns
,没有任何变量名。有些解析器可能不接受它。给出一些虚拟名称(例如: xmlns:abc
)。
在结束元素中,</
之间有空格。和元素名称。
更正以下所有内容:
</getLastLocation>
</soap12:Body>
(同时删除 :
和 Body
之间的空格)
</soap12:Envelope>
(同时删除 :
和 Envelope
之间的空格)
关于Python:SOAP API 不起作用,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54871923/