2017-01-06 37 views
0

我希望得到來自該Web服務的一些信息:http://www.mnb.hu/arfolyamok.asmx?wsdlSOAP在SQL Server

我需要的信息是GetCurrentExchangeRates方法的結果。

我用了SoapUI測試,這個請求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.mnb.hu/webservices/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:GetCurrentExchangeRates/> 
    </soapenv:Body> 
</soapenv:Envelope> 

我得到了正確的響應:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
     <GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <GetCurrentExchangeRatesResult><![CDATA[<MNBCurrentExchangeRates><Day date="2017-01-06"><Rate unit="1" curr="AUD">212,76</Rate><Rate unit="1" curr="BGN">157,05</Rate><Rate unit="1" curr="BRL">90,57</Rate><Rate unit="1" curr="CAD">218,75</Rate><Rate unit="1" curr="CHF">286,65</Rate><Rate unit="1" curr="CNY">41,83</Rate><Rate unit="1" curr="CZK">11,37</Rate><Rate unit="1" curr="DKK">41,32</Rate><Rate unit="1" curr="EUR">307,16</Rate><Rate unit="1" curr="GBP">358,91</Rate><Rate unit="1" curr="HKD">37,36</Rate><Rate unit="1" curr="HRK">40,53</Rate><Rate unit="100" curr="IDR">2,17</Rate><Rate unit="1" curr="ILS">75,42</Rate><Rate unit="1" curr="INR">4,26</Rate><Rate unit="1" curr="ISK">2,56</Rate><Rate unit="100" curr="JPY">249,83</Rate><Rate unit="100" curr="KRW">24,27</Rate><Rate unit="1" curr="MXN">13,58</Rate><Rate unit="1" curr="MYR">64,78</Rate><Rate unit="1" curr="NOK">34,12</Rate><Rate unit="1" curr="NZD">203,58</Rate><Rate unit="1" curr="PHP">5,87</Rate><Rate unit="1" curr="PLN">70,45</Rate><Rate unit="1" curr="RON">68,17</Rate><Rate unit="1" curr="RSD">2,48</Rate><Rate unit="1" curr="RUB">4,88</Rate><Rate unit="1" curr="SEK">32,16</Rate><Rate unit="1" curr="SGD">202,09</Rate><Rate unit="1" curr="THB">8,12</Rate><Rate unit="1" curr="TRY">80,08</Rate><Rate unit="1" curr="UAH">10,74</Rate><Rate unit="1" curr="USD">289,75</Rate><Rate unit="1" curr="ZAR">21,26</Rate></Day></MNBCurrentExchangeRates>]]></GetCurrentExchangeRatesResult> 
     </GetCurrentExchangeRatesResponse> 
    </s:Body> 
</s:Envelope> 

我嘗試從SQL Server這樣讀取該信息(我創建一個過程因爲它看到下面的代碼)

CREATE proc [dbo].[spHTTPRequest] 
     @URI varchar(2000) = '',  
     @methodName varchar(50) = '', 
     @requestBody varchar(8000) = '', 
     @SoapAction varchar(255), 
     @UserName nvarchar(100), -- Domain\UserName or UserName 
     @Password nvarchar(100), 
     @responseText varchar(8000) output 
AS 
    SET NOCOUNT ON 

    IF @methodName = '' 
    BEGIN 
     select FailPoint = 'Method Name must be set' 
     return 
    END 

    SET @responseText = 'FAILED' 

    DECLARE @objectID int 
    DECLARE @hResult int 
    DECLARE @source varchar(255), @desc varchar(255) 
EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP', @objectID OUT 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
        source = @source, 
        description = @desc, 
        FailPoint = 'Create failed', 
        MedthodName = @methodName 
     goto destroy 
     return 
END 
-- open the destination URI with Specified method 
EXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false', @UserName, @Password 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'Open failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 
-- set request headers 
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml;charset=UTF-8' 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'SetRequestHeader failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 
-- set soap action 
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'SOAPAction', @SoapAction 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'SetRequestHeader failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 
declare @len int 
set @len = len(@requestBody) 
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'SetRequestHeader failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 

-- send the request 
EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'Send failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 
declare @statusText varchar(1000), @status varchar(1000) 
-- Get status text 
exec sp_OAGetProperty @objectID, 'StatusText', @statusText out 
exec sp_OAGetProperty @objectID, 'Status', @status out 
select @status, @statusText, @methodName 
-- Get response text 
exec sp_OAGetProperty @objectID, 'responseText', @responseText out 
IF @hResult <> 0 
BEGIN 
     EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT 
     SELECT  hResult = convert(varbinary(4), @hResult), 
      source = @source, 
      description = @desc, 
      FailPoint = 'ResponseText failed', 
      MedthodName = @methodName 
     goto destroy 
     return 
END 
destroy: 
     exec sp_OADestroy @objectID 
SET NOCOUNT OFF 

GO 
*/ 

declare @xmlOut varchar(8000) 
Declare @RequestText as varchar(8000); 
set @RequestText= 
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.mnb.hu/webservices/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:GetCurrentExchangeRates/> 
    </soapenv:Body> 
</soapenv:Envelope>' 

exec spHTTPRequest 'http://www.mnb.hu/arfolyamok.asmx?wsdl', 'post', @RequestText,'http://tempuri.org/CreateOrderForMe','', '', @xmlOut out 

但我總是得到一個「內部服務器」的錯誤。有人可以給我一個解決方案嗎?

謝謝!

+0

請更具體一些。什麼是錯誤? –

+0

GOTOs ???真???爲什麼在goto之後有代碼? –

+0

我得到了HTTP500 - 內部服務器錯誤 – NemoPeti

回答

0

我在執行我的程序時遇到同樣的問題。似乎是在Windows 2012和Windows 2007之間的MSXML2.HTTP方法有些區別例如發送的作品作爲2012如下:

DECLARE @send NVARCHAR(4000) = 'send("' + REPLACE(@requestBody, '"', '''') + '")'; 
EXEC @hResult = sp_OAMethod @objectID, @send 

不像

sp_OAMethod @objectID, 'send', null, @requestBody 
在你的程序

+0

謝謝,最後我使用類似的東西:) – NemoPeti