FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Peticion por URL
Posts: 198
Joined: Fri Nov 18, 2005 07:34 PM
Peticion por URL
Posted: Mon Aug 10, 2020 10:45 PM

Hola, como puedo mandar peticiones a una URL y obtener respuesta de las mismas ?
por ejemplo

https://192.168.0.2/ServerPos/index.php? operation=doTestname=adminpassword=123
necesito obtener una respuesta de esa petici贸n, la pregunta es:
1.-Como hago la petici贸n en formato JSON
2.-Como obtengo la respuesta

Ya busqu茅 algunos ejemplos pero no he logrado nada, es necesario alguna librer铆a o conector ?
Gracias por su tiempo, saludos.

oHttp:= CreateObject("Microsoft.XMLHTTP")
cUrl = "https://192.168.0.2/ServerPos/index.php?"
oHttp:Open( "POST", cUrl, .F.)
oHttp:setRequestHeader( "Content-type", "text/json" )
cJson :='{"name":"admin","password":"123"}'
oHttp:Send(cJson) ("Aqu铆 sale error de conexi贸n")

&&
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Peticion por URL
Posted: Tue Aug 11, 2020 08:36 AM
servicomver wrote:Hola, como puedo mandar peticiones a una URL y obtener respuesta de las mismas ?
por ejemplo
https://192.168.0.2/ServerPos/index.php? operation=doTest&name=admin&password=123
necesito obtener una respuesta de esa petici贸n, la pregunta es:
1.-Como hago la petici贸n en formato JSON
2.-Como obtengo la respuesta

Ya busqu茅 algunos ejemplos pero no he logrado nada, es necesario alguna librer铆a o conector ?
Gracias por su tiempo, saludos.

oHttp:= CreateObject("Microsoft.XMLHTTP")
cUrl = "https://192.168.0.2/ServerPos/index.php?"
oHttp:Open( "POST", cUrl, .F.)
oHttp:setRequestHeader( "Content-type", "text/json" )
cJson :='{"name":"admin","password":"123"}'
oHttp:Send(cJson) ("Aqu铆 sale error de conexi贸n")


Lo que te falta es mirar la respuesta obtenida
Code (fw): Select all Collapse
聽 聽 聽 聽 聽 聽local cResponse 聽:= ""
聽 聽 聽 聽 聽 聽local cMyRespuesta

.../...
// Tu codigo anterior, y despu茅s del Send, esto

聽 聽 聽 聽 聽 聽cResponse := ohttp:responseText
聽 聽 聽 聽 聽 聽 cMyRespuesta 聽:= hash()
聽 聽 聽 聽 聽 聽 hb_jsondecode( cResponse , @cMyrespuesta ) 
聽 聽 聽 聽 聽 聽 XBrowse( cMyRespuesta ) 聽 聽 聽 聽 // ? cMyRespuesta
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 198
Joined: Fri Nov 18, 2005 07:34 PM
Re: Peticion por URL
Posted: Wed Aug 12, 2020 04:20 PM

Muchisimas gracias, efectivamente no recogia la respuesta. Otra consulta:

hay alguna forma de usar hb_jsonEncode y hb_jsonDecode en xHarbour ?, que tengo que hacer para usarlas ?

Gracias

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Peticion por URL
Posted: Wed Aug 12, 2020 09:35 PM

They are available in xHarbour too.

EMG

Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: Peticion por URL
Posted: Fri Oct 09, 2020 02:11 PM

Hi, i am fivewin/xharbour programmer and i am trying to capture the response from an http request ( i need to send an address and obtain the POSTAL ITALIAN CODE).
I tried this
Function http(therequest)
Local oHttp
default therequest:= "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"

Try
  • oHttp := CreateObject( "winhttp.winhttprequest.5.1" )
    oHttp:= CreateObject("Microsoft.XMLHTTP")

    Catch
    MsgInfo("errore connessione msxml2, uso microsoft ")
    oHttp := CreateObject( 'Microsoft.XMLHTTP' )
    End

oHttp:Open( 'GET', therequest, .F. )
oHttp:SetRequestHeader( "Content-Type","application/json")
oHttp:Send()

Try
cResp := oHttp:ResponseBody
Catch
MsgInfo("errore responsebody")

End
*oHttp:SetRequestHeader( "Content-Type","application/json")

SysRefresh()

MsgInfo(cResp)

Return

But i get the error from oHttp:ResponseBody : cresp dosen't exist

Can anyone help me??

Continue the discussion