所以我试图通过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)
auth工作,我用这段代码做了成功的调用,现在唯一不同的是数据XML SOAP POST。我会尝试肥皂水。
无回溯仅限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>
有什么想法吗?
乔治
答案 0 :(得分:6)
<ins0:query>
标记:
<ins0:sXML>
<![CDATA[<queryxml>
<entity>contact</entity>
<query>
<field>phone<expression op='equals'>#{phone}</expression></field>
</query>
</queryxml>]]>
</ins0:sXML>
答案 1 :(得分:2)
George,这是在webservicex.net
上调用其中一个测试Web服务的示例:
import suds
url = 'http://www.webservicex.net/stockquote.asmx?WSDL'
client = suds.client.Client(url=url)
print client.service.GetQuote('IBM')
<StockQuotes>
<Stock>
<Symbol>IBM</Symbol>
<Last>159.93</Last><Date>3/7/2011</Date><Time>4:00pm</Time>
<Change>-1.90</Change><Open>161.60</Open><High>162.98</High>
<Low>158.85</Low><Volume>5318064</Volume>
<MktCap>195.0B</MktCap><PreviousClose>161.83</PreviousClose>
<PercentageChange>-1.17%</PercentageChange>
<AnnRange>116.00 - 166.25</AnnRange>
<Earns>11.52</Earns><P-E>14.05</P-E>
<Name>International Bus</Name>
</Stock>
</StockQuotes>
您应该可以通过传入用户名和来执行HTTP基本身份验证 构造函数上的密码:
client = suds.client.Client(url=url, username='user', password='pw')
祝你好运!
答案 2 :(得分:0)
上面的回复表明他们的API“已过时”,这有点轻描淡写。话虽如此,您遇到的问题是因为您的XML格式不正确。
除了格式正确的XML主体之外,没有什么比他们期望或者更需要的了。
Eskim0手工为你制作一个身体,但问题是你可能看不到他实际做了什么。他正在更大的SOAP请求中制作一个“sXML”主体。
大多数库(perl,python和php肯定都可以这样做)提供了一种方法,你可以传入一些queryxml和“sXML”ify。这是你真正不应该手工做的事情,因为有太多的细节无法跟踪。
例如,在perl中你会这样做:
$soap->query(SOAP::Data->value($query)->name('sXML'))
会向soap端点的查询方法发出字符串$ query的sXML版本。