你如何发送SOAP请求?

时间:2012-07-12 07:19:22

标签: xml http soap

我是SOAP和xml的新手。我阅读了很多教程,但似乎没有什么是明确的。

我很困惑,一个人如何发送SOAP请求?我尝试这样做的方法是将我的SOAP请求(如下所示)保存为:testRequest.xml。

POST /MobileCashPayout.asmx HTTP/1.1
Host: 192.168.1.80
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<Payout xmlns="http://www.mycel.com/">
<Username>string</Username>
<Password>string</Password>
<referenceID>string</referenceID>
<sourceMsisdn>string</sourceMsisdn>
<destMsisdn>string</destMsisdn>
<Amount>decimal</Amount>
<MobilePin>string</MobilePin>
<cashInformation>string</cashInformation>
<merchantName>string</merchantName>
</Payout>
</soap12:Body>
</soap12:Envelope>

然后我用浏览器打开文件(testRequest.xml)以便发送它..

我收到的是一条错误消息,说明: XML解析错误:语法错误 位置:localhost / projects / test.xml 第1行,第1列:POST /MobileCashPayout.asmx HTTP / 1.1 ^

我发错了吗? 请帮帮我?

5 个答案:

答案 0 :(得分:16)

在浏览器中打开此文档不会发送请求。您有几种选择:

  • 用任何熟悉的语言编写一个小脚本,脚本应该连接到指定的服务器并发送一个POST请求,如消息中所述
  • 使用一些现有程序为您执行此操作

如果你没经验,我肯定会推荐第二种选择。我个人最喜欢的是SoapUI,请参阅here

答案 1 :(得分:7)

这篇博文对我有所帮助。 Python SOAP Request using Requests

#!/usr/bin/env python
# encoding: utf-8

import requests
from XML import XML

request = u"""<?xml version="1.0" encoding="utf-8"?>
              <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
                  <soapenv:header>
                  <soapenv:body>
                      <web:conversionrate>
                          <web:fromcurrency>GBP</web:fromcurrency>
                          <web:tocurrency>CHF</web:tocurrency>
                      </web:conversionrate>
                  </soapenv:body>
              </soapenv:header></soapenv:envelope>"""

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

headers = {"Host": "www.webservicex.net",
           "Content-Type": "text/xml; charset=UTF-8",
           "Content-Length": len(encoded_request)}

response = requests.post(url="http://www.webservicex.net/CurrencyConvertor.asmx",
                         headers = headers,
                         data = encoded_request,
                         verify=False)

print unicode(XML(response.text))

答案 2 :(得分:3)

在Linux上,您可以使用curl发送soap xml。以下是如何做到这一点:

curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: ACTION_YOU_WANT_TO_CALL" --data @FILE_NAME URL_OF_THE_SOAP_WEB_SERVICE_ENDPOINT

使用创建的testRequest.xml文件,您可以

curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: ACTION_YOU_WANT_TO_CALL" --data @testRequest.xml URL_OF_THE_SOAP_WEB_SERVICE_ENDPOINT

以下是描述完整流程的link

答案 3 :(得分:2)

据我所知,您无法在浏览器时发送soap请求。 我建议您使用像Soap UI

这样的工具

发送请求。

答案 4 :(得分:0)