所以我的問題在於嘗試使用urrlib2爲SOAP請求發佈一些數據。Python Urllib2 Post-ing Utf-8數據給出了400錯誤
數據是唯一包含任何非ascii字符的東西。
API_ENDPOINT = "https://www.foo.com/WebService/v1_2/fooService.asmx"
headers = {
'Host': 'www.foo.com',
'Content-Type': 'text/xml; charset=utf-8',
'Content-length': "%d" % len(data_xml),
'SOAPAction': '"https://www.foo.com/SaveJob"',
}
data = some xml unicode stuff
request = urllib2.Request(url=API_ENDPOINT, data=data_xml.encode("utf-8"), headers=headers)
給了我這個錯誤:
content = urllib2.urlopen(request).read()
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
我可以,但代碼已經使用基本的urrlib2編寫,所以我不想在SOAP庫中重做所有。除了像這樣發佈unicode數據的情況外,它完全可以正常工作。 – jmetz