FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to get http response text
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
How to get http response text
Posted: Sat Oct 10, 2020 08:32 AM

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

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to get http response text
Posted: Sat Oct 10, 2020 08:52 AM

Your sample works fine here.

EMG

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: How to get http response text
Posted: Sat Oct 10, 2020 11:00 AM
Code (fw): Select all Collapse
#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.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sat Oct 10, 2020 12:10 PM

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)

Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sat Oct 10, 2020 01:11 PM
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
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sat Oct 10, 2020 01:12 PM
karinha wrote:
Code (fw): Select all Collapse
#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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to get http response text
Posted: Sat Oct 10, 2020 01:18 PM
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
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sat Oct 10, 2020 01:45 PM
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)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to get http response text
Posted: Sat Oct 10, 2020 02:13 PM

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

Posts: 2170
Joined: Fri Jul 18, 2008 01:24 AM
Re: How to get http response text
Posted: Sat Oct 10, 2020 06:14 PM
karinha wrote:
Code (fw): Select all Collapse
#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.
Francisco J. Alegría P.

Chinandega, Nicaragua.



Fwxh-MySql-TMySql
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sun Oct 11, 2020 05:21 AM
FranciscoA wrote:
karinha wrote:
Code (fw): Select all Collapse
#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
Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sun Oct 11, 2020 05:25 AM
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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to get http response text
Posted: Sun Oct 11, 2020 06:51 AM

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

Posts: 37
Joined: Mon Jun 29, 2015 07:41 AM
Re: How to get http response text
Posted: Sun Oct 11, 2020 09:31 AM
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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to get http response text
Posted: Sun Oct 11, 2020 09:48 AM

Fatto. Fammi sapere.

EMG