HTTP错误415是什么意思?我做错了什么?

5

我正在发送一个SOAP POST请求,但是在使用urllib2.urlopen(req)时出现了"HTTPError: HTTP Error 415: Unsupported Media Type"错误。

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:Header>
    <AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_5/">
      <PartnerID>1</PartnerID>
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <getThresholdAndUsageInfo xmlns="http://autotask.net/ATWS/v1_5/">
    </getThresholdAndUsageInfo>
  </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/getThresholdAndUsageInfo"'
    }

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='george.lastname@domain.com',
                          passwd='mypw')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
page = urllib2.urlopen(site)                            #errors out 415 here
req = urllib2.Request(site, data, headers)
response = urllib2.urlopen(req)

我做错了什么?谢谢!
3个回答

7
headers字典中,Content-Length的值似乎不正确。
'Content-Length: len(data)' 

还有一些其他的值。

我会用以下方法修复它:

headers = {
    'Content-Type': 'application/soap+xml; charset=utf-8',
    'Host': 'webservices.autotask.net',
    'Content-Length': len(data),
    'SOAPAction': 'http://autotask.net/ATWS/v1_5/getThresholdAndUsageInfo'
}

将其更改为:'Content-Length: "%d" % len(data)' 但是仍然出现相同的问题。 - George
1
很好,你的头部信息起作用了,我现在收到了预期的响应。太棒了,谢谢! - George
为什么 Content-type 被提及了两次? - RK-

4
我知道这个问题已经解决了,但我花费了很长时间去寻找可行的解决方案,并且想把我的解决方法分享出来,以防别人也遇到同样的问题。几个小时的搜索后,我发现我正在查看的文件使用了SOAP v1.2,这可能是一个问题,因为 Suds(据我所知)还不支持v1.2。
我在此处找到了一个解决方法,使Suds认为它正在使用v1.2:SOAP 1.2 python client。我相信这不适用于所有人,因为415错误可能由许多不同的原因引起,但对我有用,而且这个问题的解决方法非常少,所以我们能得到的越多越好。我在下面贴出了对我有效的代码(该页面上有一些潜在的解决方法)。
from suds.client import Client
from suds.bindings import binding
import logging

USERNAME = 'username'
PASSWORD = 'password'

# Just for debugging purposes.
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

# Telnic's SOAP server expects a SOAP 1.2 envelope, not a SOAP 1.1 envelope
# and will complain if this hack isn't done.
binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
client = Client('client.wsdl',
    username=USERNAME,
    password=PASSWORD,
    headers={'Content-Type': 'application/soap+xml'})

# This will now work just fine.
client.service.someRandomMethod()

2
在头部中,Content-Type被列了两次。
您发送的消息正在使用SOAP 1.1命名空间,这将与第二个Content-Type(text/xml)匹配。根据错误信息,我猜测第一个Content-Type(application/soap+xml)实际上是发送到服务器的SOAP 1.2消息。移除第一个Content-Type,如果您的服务器确实希望收到SOAP 1.1消息,则问题应该会解决。

当我删除 content-type 后,我会得到一个 "AttributeError: 'set' object has no attribute 'items'" @ req = urllib2.Request(site, data, headers)。服务器可以接受 SOAP 1.1 和 SOAP 1.2。 - George

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接