Dois exemplos de web services.
Enviado: 25 Mar 2022 14:48
Olá!
Peguei no grupo internacional.
Usando a CURL Lib do Harbour Linux e Windows.
Saudações,
Itamar M. Lins Jr.
Peguei no grupo internacional.
Código: Selecionar todos
Hola,
Un ejemplo de Get :
******************************************************
#xcommand TRY => BEGIN SEQUENCE WITH {| oErr | Break( oErr ) }
#xcommand CATCH [<!oErr!>] => RECOVER [USING <oErr>] <-oErr->
#xcommand ENDTRY => END
Funct Main()
LOCAL ohttp
LOCAL cUrl:=""
LOCAL JRespuesta:=""
LOCAL Id
LOCAL Name
LOCAL eMail
// curl -i -H "Accept:application/json" -H "Content-Type:application/json" -XGET "https://gorest.co.in/public/v2/users"
cUrl :="https://gorest.co.in/public/v2/posts"
ohttp := CreateObject( "MSXML2.XMLHTTP" )
ohttp:Open( "GET" ,cUrl,.F.)
ohttp:SetRequestHeader("content-type" , "application/json" )
ohttp:SetRequestHeader("Accept" , "application/json" )
ohttp:SetRequestHeader("Authorization", "Bearer eaa9b2c22c86399d3963f5b240da976927965621ad82bd512b63313e6d66965e" )
TRY
ohttp:Send()
CATCH
Alert("No Se pudo Enviar solicitud ")
return JRespuesta
END
JRespuesta:=ohttp:responseText
Alert("respuesta;"+JRespuesta)
aHasRes := {=>}
hb_jsondecode(JRespuesta ,@aHasRes)
For i := 1 To Len(aHasRes)
Hb_Alert(aHasRes[i]["id"])
Hb_Alert(aHasRes[i]["user_id"])
Hb_Alert(aHasRes[i]["title"])
Hb_Alert(aHasRes[i]["body"])
Next
RETURN Nil
Código: Selecionar todos
here a SOAP example. I can communicate with a mono webservice either locally
(localhost) or over the network:
------------------------------
#include "hbcurl.ch"
#include "common.ch"
#include "fileio.ch"
function fHttpExecute(cxml)
local endpointUrl,curlHandle,curlErr
local aHeader,chpmserv,cc1
cc1 := ""
cservice := "InformationsService"
caction := "LiefereKernversion"
chpmserv := "localhost"
endpointUrl = "http://"+chpmserv+":22220/"+cservice+".asmx"
aHeader := {}
AADD(aHeader,"Accept-Encoding: gzip,deflate" )
AADD(aHeader,"Content-Type: application/soap+xml;charset=UTF-8")
AADD(aHeader,'action="http://some-website.de/'+caction+'"')
curlHandle := curl_easy_init()
if !empty(curlHandle)
/* Specify the Header data */
curl_easy_setopt(curlHandle,HB_CURLOPT_HTTPHEADER,aHeader)
/* Set the endpoint to send the POST to */
curl_easy_setopt(curlHandle, HB_CURLOPT_URL, endpointUrl)
/* Setup response data */
curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )
/* Specify the POST data */
curl_easy_setopt(curlHandle, HB_CURLOPT_POST, 1)
curl_easy_setopt(curlHandle, HB_CURLOPT_POSTFIELDS, cxml)
/* Do everything */
curlErr := curl_easy_perform(curlHandle)
/* Report any errors */
if empty(curlErr)
/* store response in variable */
cc1 := curl_easy_dl_buff_get( curlHandle )
else
? curl_easy_strerror(curlErr)
endif
else
? "No handle"
endif
if !empty(curlHandle)
/* Clean-up libcurl */
curl_global_cleanup( curlHandle )
else
? "Error"
endif
if empty(cc1)
? "Error"
endif
return cc1
-------------------------
You need to know the exact endpointUrl, the service and action. This information
is contained in the WSDL files. Sometimes it can be hard to get the header lines
in aHeader right so that the webservice accepts the message.
Itamar M. Lins Jr.