【问题标题】:xml soap code that works in c#projects in visual studio but not in Python在 Visual Studio 的 c#projects 中工作但在 Python 中不工作的 xml 肥皂代码
【发布时间】:2019-07-25 09:47:44
【问题描述】:

我知道 c# 和 Python 是两种不同的语言,但是我认为 xml soap 代码应该大致相似,但我看不到让它工作。在 c# 中工作的 url 最终只是获取了我的网页代码,我正在使用 POST 并且想要输入身份验证和输入以在服务器中检索信息。我该怎么做呢

我尝试了各种 url,各种请求格式,但是我不太熟悉 python 和 soap 的交互方式,所以我所能做的就是在线搜索并调整现有代码,但它们似乎都在连接时发出错误(我认为这是一个 url 问题)或检索网页本身的代码。

 import requests 
    from xml.etree import ElementTree as ET

    url= "http://website/websiteAPI.asmx?op=GetInformation" headers = {
    'Content-Type' : 'application/soap+xml' 'charset=utf-8' 
    'SOAPAction' "http://website/websiteApi/GetInformation"}
    body = """

    <?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:Body>
        <GetAssetListByType 
             xmlns="http://website/websiteApi/GetInformation">
          <user>test</user>
          <pass>456</pass>
          <RanCode>ver</RanCode>
        </GetAssetListByType>   </soap:Body> </soap:Envelope>"""

    response = requests.post(url, data=body,headers=headers) 
print (response.content) 
print (response.status_code)

我希望登录并能够从服务器检索输出,但是我收到了错误 500/404/301 或网页代码。

【问题讨论】:

  • 你为什么from xml.etree import ElementTree as ET
  • http 有标头,c# 中的默认标头可能与 xml soap 和 python 不同。要找到问题的根本原因,您需要使用像 wireshark 或 fiddler 这样的嗅探器并比较工作代码和非工作代码。修复使非工作 http 标头看起来与工作 http 标头完全相同。

标签: c# python xml soap


【解决方案1】:

好吧,不知怎的,我用这段代码让它工作了,我会很诚实地说我不知道​​它实际上是如何工作的。

import requests
request = u"""

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://website/websiteApi">
   <soapenv:Header/>
   <soapenv:Body>
      <theAPI>

         <user></pass>

         <pass></pass>
      </theAPI>
   </soapenv:Body>
</soapenv:Envelope>"""

encoded_request = request.encode('utf-8')

headers = {"Host": "172.31.99.16",
           "Content-Type": "text/xml; charset=UTF-8",
           "Content-Length": "length"}

response = requests.post(url="http://website/websiteAPI.asmx?",
                         headers = headers,
                         data = encoded_request,
                         verify=False)
print (response.text)
print (response.status_code)

这可以作为soap xml请求的.py代码

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2014-05-14
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多