Peticion por URL

Peticion por URL

Postby servicomver » 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=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")
servicomver
 
Posts: 179
Joined: Fri Nov 18, 2005 7:34 pm

Re: Peticion por URL

Postby cnavarro » Tue Aug 11, 2020 8: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: Select all  Expand view

           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
User avatar
cnavarro
 
Posts: 6501
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Peticion por URL

Postby servicomver » Wed Aug 12, 2020 4: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
servicomver
 
Posts: 179
Joined: Fri Nov 18, 2005 7:34 pm

Re: Peticion por URL

Postby Enrico Maria Giordano » Wed Aug 12, 2020 9:35 pm

They are available in xHarbour too.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8338
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Peticion por URL

Postby lorenzoazz » Fri Oct 09, 2020 2: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??
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 15 guests