【问题标题】:passing soap envelope in Python works on urllib but not on requests在 Python 中传递肥皂信封适用于 urllib,但不适用于请求
【发布时间】:2016-07-22 23:55:41
【问题描述】:

我在 Python2 中使用 urllib 传递一个肥皂信封,它工作正常,但在升级到 Python3 和请求后,事务失败。失败的具体错误是“服务器无法为请求提供服务,因为媒体类型不受支持”。以下是每个的内容/命令:

Python2/urllib

request = urllib2.Request(self._url, xml, request_headers)

每个变量的内容

self._url:

https://cert.api2.heartlandportico.com/Hps.Exchange.PosGateway/PosGatewayService.asmx?wsdl

xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><PosRequest xmlns="http://Hps.Exchange.PosGateway"><Ver1.0><Header><SecretAPIKey>skapi_cert_MYl2AQAowiQxxxxxxxizOP2jcX9BrEMqQ</SecretAPIKey><DeveloperID>000000</DeveloperID><VersionNbr>0000</VersionNbr></Header><Transaction><CreditSale><Block1><AllowDup>Y</AllowDup><AllowPartialAuth>N</AllowPartialAuth><Amt>1.15</Amt><CardHolderData><CardHolderFirstName>evan</CardHolderFirstName><CardHolderLastName>stone</CardHolderLastName><CardHolderPhone>9405947406</CardHolderPhone><CardHolderAddr>417 Neverland</CardHolderAddr><CardHolderCity>Denton</CardHolderCity><CardHolderState>TX</CardHolderState><CardHolderZip>76209</CardHolderZip></CardHolderData><CardData><TokenData><TokenValue>supt_kMKxxxxxxQacvPDvZNa</TokenValue><CardPresent>N</CardPresent><ReaderPresent>N</ReaderPresent></TokenData><TokenRequest>N</TokenRequest></CardData></Block1></CreditSale></Transaction></Ver1.0></PosRequest></soap:Body></soap:Envelope>

request_headers

{'Content-length': '1110', 'Content-type': 'text/xml; charset=UTF-8'}

Python3/请求

request = requests.post(self._url, xml, request_headers)

每个变量的内容

(同上)

注意:

Python3/requests 中的实际 request.headers.headers(显示 sent 标头)使它看起来好像我的标头字典被完全忽略了,除了关于长度的一个变量:

{'User-Agent': 'python-requests/2.10.0', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '1110', 'Connection': 'keep-alive'}

在 Python2/urllib 中,request.headers 的输出与我发送的完全相同(见下文),这让我认为请求中发生了一些事情正在改变我的标头,这就是搞砸一切的原因。想法?任何帮助将不胜感激:

{'Content-length': '1110', 'Content-type': 'text/xml; charset=UTF-8'}

【问题讨论】:

    标签: python soap http-headers


    【解决方案1】:

    哇...我一发布我的问题就明白了:显然标头不能像 urllib 中那样简单地作为请求中的位置参数——你必须指定 headers= 瞧!

    我改了这个:request = requests.post(self._url, xml, request_headers)

    对此:request = requests.post(self._url, xml, headers=request_headers)

    而且它刚刚成功。

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 2020-11-08
      • 2021-10-06
      • 2012-09-11
      • 1970-01-01
      • 2019-07-03
      • 2020-11-23
      • 1970-01-01
      • 2018-11-21
      相关资源
      最近更新 更多