2012-07-12 144 views
11

我是SOAP和xml的新手。我讀了一些教程,但似乎沒有足夠清楚。你如何發送SOAP請求?

我很高興,只是如何發送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解析錯誤:語法錯誤 位置:本地主機/項目/的test.xml 1號線,1列:POST /MobileCashPayout.asmx HTTP/1.1 ^

我是否發送了錯誤的方式? 請幫我一下嗎?

+0

XML內容以「<?xml」開頭。之前的所有內容都是HTTP標頭。你在使用什麼平臺? Java的? – 2012-07-14 14:08:49

回答

13

在瀏覽器中打開此文檔不會發送請求。您有幾種選擇:

  • 寫任何熟悉的語言小腳本,腳本應該連接到指定的服務器,併爲您的郵件
  • 提到使用一些現有的方案做派與身體POST請求爲你

如果你沒有經驗,我肯定會推薦第二個選項。我個人最喜歡的是SoapUI,見here

+0

謝謝,我對PHP很熟悉。我需要一些指導。 SoapUI並沒有真正的幫助,因爲SoapUI使用WSDL/WADL而不是所需的xml。任何指引? – SirBT 2012-07-12 20:06:53

+1

您說得對,SoapUI需要您希望使用的Web服務的定義。 Web服務通常在.wsdl文件(WSDL = Web服務描述語言)中定義,因此您只需要調用一個wsdl的MobileCashPayout服務。最有可能的是,如果您輸入 /MobileCashPayout.asmx?wsdl(您將替換爲服務所在的實際URL),可以找到它。請讓我知道你是否成功。 – 2012-07-13 06:58:32

+0

我曾嘗試過您的解決方案,但是,獲得 – SirBT 2012-07-13 18:26:50

1

當我知道瀏覽器時,您無法發送肥皂請求。 我建議你使用像Soap UI

這樣的工具來發送請求。

+0

謝謝你,你的回覆很有見地,因爲你介紹了我的肥皂用戶界面。問題是上面的初始SOAP代碼是用XML編寫的,而不是用WSDL/WADL編寫的。肥皂UI使用WSDL/WADL而不是所需的XML。有任何想法嗎? – SirBT 2012-07-12 20:02:12

+0

SOAP UI支持XML格式化和可編輯的請求,並提供XML格式化響應。您可以將您的XML複製並粘貼到SOAP請求窗口中。 – lubosdz 2014-05-07 12:23:39

6

此博客文章幫助了我。 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)) 
+0

僅供參考,它是鏈接唯一的答案,必須包含來自鏈接的重要細節/代碼,因爲鏈接將來可能不再存在! – 2014-07-16 18:22:19

+0

已複製來自博客的代碼。 – JohnMudd 2014-07-17 19:35:04

+2

現在看起來像一個很好的答案! – 2014-07-18 05:05:33

2

在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描述全過程。

+0

我會永遠喜歡捲曲。簡單。 – 2017-08-04 10:08:43