Python - SOAP服务器无法识别HTTP标头的值

时间:2017-01-06 23:27:36

标签: python soap

第一次使用SOAP,我发现了多个类似错误消息的实例,但似乎没有任何内容适用于我。

代码是用Python(3.5)编写的。

以下是我试图提出请求的网站的确切示例:

POST /WS/OpenAPI.asmx HTTP/1.1
Host: bms.kaseya.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://bms.kaseya.com/Authenticate"

<?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>
    <Authenticate xmlns="https://bms.kaseya.com/">
      <companyName>string</companyName>
      <userName>string</userName>
      <password>string</password>
    </Authenticate>
  </soap:Body>
</soap:Envelope>

我正在使用Python中的请求,所有似乎都很顺利:

import requests

companyName = 'My Company'
userName = 'My Username'
password = 'My Password'

url = 'https://bms.kaseya.com/WS/OpenAPI.asmx'

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>
    <Authenticate xmlns="https://bms.kaseya.com/">
      <companyName>{}</companyName>
      <userName>{}</userName>
      <password>{}</password>
    </Authenticate>
  </soap:Body>
</soap:Envelope>'''.format(companyName,userName,password)

headers = {'Host':'bms.kaseya.com',
           'Content-Type':'text/xml; charset=utf-8',
           'Content-Length':str(len(body)),
           'SOAPAction':'"https://bms.kaseya.com/Authenticate"'}

r = requests.post(url, data=body, headers=headers)
print(r.text)

除了我得到的部分:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope 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"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: https://bms.kaseya.com/Authenticate.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

从我发现的似乎SOAPAction的url是错误的???我真的以他们为榜样,只是插入我的实时信息,我不知道我哪里出错,如果它是我的话。

1 个答案:

答案 0 :(得分:0)

无视它已被修复。我的代码很好,正如预期的那样,这是由于他们的文档不好。稍微修复一些URL,一切都按预期工作!

原来我们选择SOAP 1.2,正确的例子应该是:

POST /WS/OpenAPI.asmx HTTP/1.1
Host: bms.kaseya.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?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>
    <Authenticate xmlns="https://vorexlogin.com/">
      <companyName>string</companyName>
      <userName>string</userName>
      <password>string</password>
    </Authenticate>
  </soap12:Body>
</soap12:Envelope>