Python+Soap+XMLHttpRequests

2024-10-01 15:47:50 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在尝试做一个SOAP请求这里是我的代码:

# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='webservices.autotask.net',
                          uri='https://webservices.autotask.net/atservices/1.5/atws.asmx',
                          user=username,
                          passwd=password)
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
page = urllib2.urlopen('https://webservices.autotask.net/atservices/1.5/atws.asmx')
print (page.read(100))


SM_TEMPLATE = """<?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:Header>
    <AutotaskIntegrations xmlns="https://webservices.autotask.net/ATWS/v1_5/">
      <PartnerID>string</PartnerID>
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <getThresholdAndUsageInfo xmlns="https://webservices.autotask.net/ATWS/v1_5/">
      <sXML>string</sXML>
    </getThresholdAndUsageInfo>
  </soap:Body>
</soap:Envelope>

"""
SoapMessage = SM_TEMPLATE%()

print SoapMessage


# construct and send the header
webservice = httplib.HTTPSConnection("http://webservices.autotask.net", 443)
webservice.putrequest("POST", "/ATWS/v1_5/atws.asmx")
webservice.putheader("Host", "http://webservices.autotask.net")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(SoapMessage))
webservice.putheader("SOAPAction", "\"\"")
print("HEADERS")
webservice.endheaders()
webservice.send(SoapMessage)

# get the response

statuscode, statusmessage, header = webservice.getreply()
print ("Response: ", statuscode, statusmessage)
print ("headers: ", header)
res = webservice.getfile().read()
print (res)

我得到的错误是(顺便说一句,第69行是webservice.endheaders()):

^{pr2}$

有什么想法吗?在

编辑:我最终在他们的论坛上找到了一个.Net库。图书馆为我的需要服务。在


Tags: httpsauthwebhttpnetserviceopenerurllib2
1条回答
网友
1楼 · 发布于 2024-10-01 15:47:50

HTTPSConnection接受主机名,而不是URL,因此它应该是

webservice = httplib.HTTPSConnection("webservices.autotask.net", 443)

我相信主机头是为您添加的,所以您可以跳过它,但如果不是,那也应该是主机名,而不是URL。在

相关问题 更多 >

    热门问题