Page 1 of 2

How to get http response text

PostPosted: Sat Oct 10, 2020 8:32 am
by lorenzoazz
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 does not exist

Can anyone help me??

lorenzoazz

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 8:52 am
by Enrico Maria Giordano
Your sample works fine here.

EMG

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 11:00 am
by karinha
Code: Select all  Expand view

#Include "FiveWin.ch"
#Include "Fileio.ch"

#ifdef __XHARBOUR__  // xHarbour
   #include "tip.ch"
#endif

FUNCTION http( therequest )

   LOCAL oHttp, cResp

   DEFAULT therequest := "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"

   /*
   Try

      oHttp := CreateObject( "Microsoft.XMLHTTP" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      oHttp := CreateObject( 'Microsoft.XMLHTTP' )

   End
   */


#ifdef __XHARBOUR__  // xHarbour

   Try

      // oHttp := CreateObject( "Microsoft.XMLHTTP" )     // no funciona
      oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" ) // funciona.

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      Return Nil

   End

#else

   Try

      oHttp := win_OleCreateObject( "MSXML2.ServerXMLHTTP.5.0" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      RETURN Nil

   End

#endif


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

   Try

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End
   */


   Try

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

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End

   SysRefresh()

   MsgInfo( cResp )

RETURN NIL

// fin
 


Regards, saludos.

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 12:10 pm
by lorenzoazz
I tried the example posted but i still get the same error, the problem rises when i call ohttp:responsebody


(seems that ohttp:responsebody is null)

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 1:11 pm
by lorenzoazz
Enrico Maria Giordano wrote:Your sample works fine here.

EMG


Non funziona , l'errore è causato da ohttp:responsebody che sembra non essere accessibile e comunque non ritorna nessuna stringa (credo sia null)
Prima non ho errori !
Lorenzo

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 1:12 pm
by lorenzoazz
karinha wrote:
Code: Select all  Expand view

#Include "FiveWin.ch"
#Include "Fileio.ch"

#ifdef __XHARBOUR__  // xHarbour
   #include "tip.ch"
#endif

FUNCTION http( therequest )

   LOCAL oHttp, cResp

   DEFAULT therequest := "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"

   /*
   Try

      oHttp := CreateObject( "Microsoft.XMLHTTP" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      oHttp := CreateObject( 'Microsoft.XMLHTTP' )

   End
   */


#ifdef __XHARBOUR__  // xHarbour

   Try

      // oHttp := CreateObject( "Microsoft.XMLHTTP" )     // no funciona
      oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" ) // funciona.

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      Return Nil

   End

#else

   Try

      oHttp := win_OleCreateObject( "MSXML2.ServerXMLHTTP.5.0" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      RETURN Nil

   End

#endif


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

   Try

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End
   */


   Try

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

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End

   SysRefresh()

   MsgInfo( cResp )

RETURN NIL

// fin
 


Regards, saludos.


--------------------------------------------------------
I tried the example posted but i still get the same error, the problem rises when i call ohttp:responsebody


(seems that ohttp:responsebody is null)
Regards

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 1:18 pm
by Enrico Maria Giordano
lorenzoazz wrote:
Enrico Maria Giordano wrote:Your sample works fine here.

EMG


Non funziona , l'errore è causato da ohttp:responsebody che sembra non essere accessibile e comunque non ritorna nessuna stringa (credo sia null)
Prima non ho errori !
Lorenzo


Qui funziona regolarmente e ohttp:responsebody contiene la pagina web di risposta.

EMG

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 1:45 pm
by lorenzoazz
Enrico Maria Giordano wrote:
lorenzoazz wrote:
Enrico Maria Giordano wrote:Your sample works fine here.

EMG


Non funziona , l'errore è causato da ohttp:responsebody che sembra non essere accessibile e comunque non ritorna nessuna stringa (credo sia null)
Prima non ho errori !
Lorenzo


Qui funziona regolarmente e ohttp:responsebody contiene la pagina web di risposta.

EMG


Forse devo linkare qualche libreria che non ho caricato? ( ho incluso anche tip.ch)

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 2:13 pm
by Enrico Maria Giordano
No, non credo. Vuoi che ti mandi il mio EXE così lo provi da te? Se sì, mandami un indirizzo email che accetti gli EXE.

EMG

Re: How to get http response text

PostPosted: Sat Oct 10, 2020 6:14 pm
by FranciscoA
karinha wrote:
Code: Select all  Expand view

#Include "FiveWin.ch"
#Include "Fileio.ch"

#ifdef __XHARBOUR__  // xHarbour
   #include "tip.ch"
#endif

FUNCTION http( therequest )

   LOCAL oHttp, cResp

   DEFAULT therequest := "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"

   /*
   Try

      oHttp := CreateObject( "Microsoft.XMLHTTP" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      oHttp := CreateObject( 'Microsoft.XMLHTTP' )

   End
   */


#ifdef __XHARBOUR__  // xHarbour

   Try

      // oHttp := CreateObject( "Microsoft.XMLHTTP" )     // no funciona
      oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" ) // funciona.

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      Return Nil

   End

#else

   Try

      oHttp := win_OleCreateObject( "MSXML2.ServerXMLHTTP.5.0" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      RETURN Nil

   End

#endif


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

   Try

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End
   */


   Try

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

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End

   SysRefresh()

   MsgInfo( cResp )

RETURN NIL

// fin
 


Regards, saludos.


Lorenzo:
El codigo modificado por Joao (Karinha), funciona sin problemas, aquí.
Da la impresión de que el error que obtienes con tu codigo ("cresp does not exist") , es debido a variable no declarada.

Re: How to get http response text

PostPosted: Sun Oct 11, 2020 5:21 am
by lorenzoazz
FranciscoA wrote:
karinha wrote:
Code: Select all  Expand view

#Include "FiveWin.ch"
#Include "Fileio.ch"

#ifdef __XHARBOUR__  // xHarbour
   #include "tip.ch"
#endif

FUNCTION http( therequest )

   LOCAL oHttp, cResp

   DEFAULT therequest := "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"

   /*
   Try

      oHttp := CreateObject( "Microsoft.XMLHTTP" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      oHttp := CreateObject( 'Microsoft.XMLHTTP' )

   End
   */


#ifdef __XHARBOUR__  // xHarbour

   Try

      // oHttp := CreateObject( "Microsoft.XMLHTTP" )     // no funciona
      oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" ) // funciona.

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      Return Nil

   End

#else

   Try

      oHttp := win_OleCreateObject( "MSXML2.ServerXMLHTTP.5.0" )

   Catch

      MsgInfo( "errore connessione msxml2, uso microsoft " )

      RETURN Nil

   End

#endif


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

   Try

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End
   */


   Try

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

      cResp := oHttp:ResponseBody

   Catch

      MsgInfo( "errore responsebody" )

   End

   SysRefresh()

   MsgInfo( cResp )

RETURN NIL

// fin
 


Regards, saludos.


Lorenzo:
El codigo modificado por Joao (Karinha), funciona sin problemas, aquí.
Da la impresión de que el error que obtienes con tu codigo ("cresp does not exist") , es debido a variable no declarada.

-------------------------------------------

thank you
now I have declared
local cresp:="vuoto"

but cresp remains "vuoto" (unchanged)
the problem cames when i call oHttp:ResponseBody

Re: How to get http response text

PostPosted: Sun Oct 11, 2020 5:25 am
by lorenzoazz
Enrico Maria Giordano wrote:No, non credo. Vuoi che ti mandi il mio EXE così lo provi da te? Se sì, mandami un indirizzo email che accetti gli EXE.

EMG


Grazie Enrico ma io devo capire il motivo dell'errore per poter programmare la logica successiva, in realtà poi dovrei creare un DOM e poi cercare tra i nodi del documento html ottenuto in risposta.

Lorenzo

Re: How to get http response text

PostPosted: Sun Oct 11, 2020 6:51 am
by Enrico Maria Giordano
Che versione di Windows stai usando? Dall'errore sembra che la DLL relativa al componente non supporti quella proprietà, cosa che mi sembra assurda. E comunque la prova dell'EXE può essere utile per avere qualche indizio in più.

EMG

Re: How to get http response text

PostPosted: Sun Oct 11, 2020 9:31 am
by lorenzoazz
Enrico Maria Giordano wrote:Che versione di Windows stai usando? Dall'errore sembra che la DLL relativa al componente non supporti quella proprietà, cosa che mi sembra assurda. E comunque la prova dell'EXE può essere utile per avere qualche indizio in più.

EMG

uso windows 10

ok inviami l'exe per provare via mail a lorenzoazzolini@gmail.com rinominando l'exe a .txt
Grazie

Re: How to get http response text

PostPosted: Sun Oct 11, 2020 9:48 am
by Enrico Maria Giordano
Fatto. Fammi sapere.

EMG