python - XML SOAP POST 错误,我做错了什么?

标签 python xml soap post

所以我正在尝试通过 XML SOAP POST 执行 API 调用,我得到的错误是:“对象引用未设置为对象的实例”

site = 'https://webservices.autotask.net/atservices/1.5/atws.asmx'
data = """<?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>
    <queryxml>
      <entity>contact</entity>
        <query>
          <field>firstname<expression op="equals">George</expression>
          </field>
        </query>
    </queryxml>
  </soap:Body>
</soap:Envelope>"""

headers = {
    'Content-Type': 'application/soap+xml; charset=utf-8',
    'Host': 'webservices.autotask.net',
    'Content-Type': 'text/xml; charset=utf-8',
    'Content-Length': len(data),
    'SOAPAction': "http://autotask.net/ATWS/v1_5/query"
    } 
site = 'https://webservices.autotask.net/atservices/1.5/atws.asmx'
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='webservices.autotask.net',
                          uri=site,
                          user='user,
                          passwd='pw')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
page = urllib2.urlopen(site)
print(data)
req = urllib2.Request(site, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
print(the_page)

身份验证有效,我已使用此代码成功调用,现在唯一不同的是数据 XML SOAP POST。我会试试 SOAP 水。

没有 Traceback 只有 web 服务器错误:

打印出我发送的 XML SOAP POST:

<?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> <queryxml> <entity>contact</entity> <query> <field>firstname<expression op="equals">George</expression> </field> </query> </queryxml> </soap:Body> </soap:Envelope>

响应:

<?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><queryResponse xmlns="http://autotask.net/ATWS/v1_5/"><queryResult><ReturnCode>-1</ReturnCode><EntityResults /><EntityResultType /><Errors><ATWSError><Message>Object reference not set to an instance of an object.</Message></ATWSError><ATWSError><Message>Error reading in Query XML.</Message></ATWSError></Errors><EntityReturnInfoResults /></queryResult></queryResponse></soap:Body></soap:Envelope>

有什么想法吗?

乔治

最佳答案

Autotask 在 IIS 6 上有一个古老的 API... 要处理 Microsoft crapstack,您必须转义您作为 CDATA 推送到那里的 XML。这是对我有用的 SOAP 主体,在

<ins0:query> 

标签:

<ins0:sXML>
<![CDATA[<queryxml>
<entity>contact</entity>
<query>
<field>phone<expression op='equals'>#{phone}</expression></field>
</query>
</queryxml>]]>
</ins0:sXML>

关于python - XML SOAP POST 错误,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227333/

相关文章:

Python/PyQT : How can I truncate a text in QLineEdit

xml - 如何更改 Sublime Text2 中 JSON `Key` 的颜色

java - 通过 SSL 的 SOAP 网络服务连接

python - Python Pandas 中键等于另一列值的累积计数

python - 你如何在 PyQt 的另一个线程中运行 Matplotlib

Python 3 获取 HTTP 页面

java - 将对象存储到 HashMap 中

xml - 我正在使用 Intellij IDEA。如何为 scala.xml 使用的 xerces stacktrace 提供 "view source"?

PHP fatal error : "The SOAP action specified on the message, ' ', 与 HTTP SOAP 操作不匹配”

wcf - 如何使 WCF 客户端符合特定的 WS-Security