/ soap test 2
//
#include "fivewin.ch"
static cHost := "servicios.bcn.gob.ni"
static cURL1 := "https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?op=RecuperaTC_Dia"
static cURL2 := "https://servicios.bcn.gob.ni/Tc_Servicio/ServicioTC.asmx?op=RecuperaTC_Mes"
static cSoapAction1 := "http://servicios.bcn.gob.ni/RecuperaTC_Dia"
static cSoapAction2 := "http://servicios.bcn.gob.ni/RecuperaTC_Mes"
static oHttp
//----------------------------------------------------------------------------//
function Main()
local aRates := {}
SET DATE ITALIAN
SET CENTURY ON
TRY
oHttp := CreateObject( 'MSXML2.XMLHTTP' )
CATCH
oHttp := CreateObject( 'Microsoft.XMLHTTP' )
END
if oHttp == nil
? "Fail"
return nil
endif
msgRun( "Reading Exchange Rates 1","PLEASE WAIT", {|| aRates := ReadRates1( {^ 2017/09/01 } ) } )
XBROWSER aRates TITLE "EXCHANGE RATES"
msgRun( "Reading Exchange Rates 2","PLEASE WAIT", {|| ReadRates2( 10, 2017 ) } )
return nil
//----------------------------------------------------------------------------//
static function ReadRates1( dDate )
local d, d2 := EOM( dDate )
local aRates := {}
for d := dDate to d2
AAdd( aRates, { d, SendHttpRequest1( makeSoapRequest1( d ) ) } )
next
return aRates
//----------------------------------------------------------------------------//
static procedure ReadRates2( nMonth, nYear )
local cRates := ""
cRates := SendHttpRequest2( makeSoapRequest2( nMonth, nYear ) )
if SavedResult( cRates )
?"save xml file with rates"
endif
return
//----------------------------------------------------------------------------//
static function MakeSoapRequest1( dDate )
local cXml
TEXT INTO cXml
<?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>
<RecuperaTC_Dia xmlns="http://servicios.bcn.gob.ni/">
<Ano>YEAR</Ano>
<Mes>MONTH</Mes>
<Dia>DAY</Dia>
</RecuperaTC_Dia>
</soap:Body>
</soap:Envelope>
ENDTEXT
cXml := StrTran( cXml, chr(9), "" )
cXml := StrTran( cXml, "YEAR", cValToChar( YEAR( dDate ) ) )
cXml := StrTran( cXml, "MONTH", cValToChar( MONTH( dDate ) ) )
cXml := StrTran( cXml, "DAY", cValToChar( DAY( dDate ) ) )
return cXml
//----------------------------------------------------------------------------//
static function MakeSoapRequest2( nMonth, nYear )
local cXml
TEXT INTO cXml
<?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>
<RecuperaTC_Mes xmlns="http://servicios.bcn.gob.ni/">
<Ano>YEAR</Ano>
<Mes>MONTH</Mes>
</RecuperaTC_Mes>
</soap:Body>
</soap:Envelope>
ENDTEXT
cXml := StrTran( cXml, chr(9), "" )
cXml := StrTran( cXml, "YEAR", cValToChar( nYear ) )
cXml := StrTran( cXml, "MONTH", cValToChar( nMonth ) )
return cXml
//----------------------------------------------------------------------------//
static function SendHttpRequest1( cRequestXML )
local cContentType := "text/xml; charset=utf-8"
local cUserAgent := "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
local cXml := cRequestXML
//
local cResponseBody, nStatus, cStatusText
local nResult := 0
oHttp:Open( 'POST', cURL1, .F. )
oHttp:SetRequestHeader( "Connection:", "Keep-Alive" )
oHttp:SetRequestHeader( "Host", cHost )
oHttp:SetRequestHeader( "SOAPAction", cSOAPAction1 )
oHttp:setRequestHeader( "User-Agent", cUserAgent )
oHttp:SetRequestHeader( "Content-Type", cContentType )
oHttp:SetRequestHeader( "Content-Length", LTrim( Str( Len( cXml ) ) ) )
TRY
oHttp:Send( cXml )
SysRefresh()
CATCH
MsgInfo("Could not send the XML documnent." +CRLF+ "The reason could be either the internet connection failed or its a server problem")
QUIT
END
cResponseBody := oHttp:ResponseBody
oHttp:getAllResponseHeaders()
nStatus := oHttp:Status
cStatusText := oHttp:StatusText
if nStatus == 200
nResult := ReadResult( cResponseBody )
endif
return nResult
//----------------------------------------------------------------------------//
static function SendHttpRequest2( cRequestXML )
local cContentType := "text/xml; charset=utf-8"
local cUserAgent := "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
local cXml := cRequestXML
//
local cResponseBody, nStatus, cStatusText
local cResult := ""
oHttp:Open( 'POST', cURL2, .F. )
oHttp:SetRequestHeader( "Connection:", "Keep-Alive" )
oHttp:SetRequestHeader( "Host", cHost )
oHttp:SetRequestHeader( "SOAPAction", cSOAPAction2 )
oHttp:setRequestHeader( "User-Agent", cUserAgent )
oHttp:SetRequestHeader( "Content-Type", cContentType )
oHttp:SetRequestHeader( "Content-Length", LTrim( Str( Len( cXml ) ) ) )
TRY
oHttp:Send( cXml )
SysRefresh()
CATCH
MsgInfo("Could not send the XML documnent." +CRLF+ "The reason could be either the internet connection failed or its a server problem")
QUIT
END
cResponseBody := oHttp:ResponseBody
oHttp:getAllResponseHeaders()
nStatus := oHttp:Status
cStatusText := oHttp:StatusText
if nStatus == 200
cResult := cResponseBody
endif
return cResult
//----------------------------------------------------------------------------//
static function ReadResult( cXml )
return Val( BeforAtNum( "</RecuperaTC_DiaResult>", AfterAtNum( "<RecuperaTC_DiaResult>", cXml, 1 ), 1 ) )
//----------------------------------------------------------------------------//
static function SavedResult( cXml )
?cXml
return ( StrFile( cXml, "tc.xml" ) > 0 )