How to get http response text

How to get http response text

Postby lorenzoazz » Sat Oct 10, 2020 8: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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby Enrico Maria Giordano » Sat Oct 10, 2020 8:52 am

Your sample works fine here.

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

Re: How to get http response text

Postby karinha » Sat Oct 10, 2020 11:00 am

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.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7214
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: How to get http response text

Postby lorenzoazz » 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)
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby lorenzoazz » Sat Oct 10, 2020 1: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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby lorenzoazz » Sat Oct 10, 2020 1:12 pm

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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby Enrico Maria Giordano » Sat Oct 10, 2020 1: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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to get http response text

Postby lorenzoazz » Sat Oct 10, 2020 1: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)
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby Enrico Maria Giordano » Sat Oct 10, 2020 2: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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to get http response text

Postby FranciscoA » Sat Oct 10, 2020 6:14 pm

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.
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2110
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: How to get http response text

Postby lorenzoazz » Sun Oct 11, 2020 5:21 am

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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby lorenzoazz » Sun Oct 11, 2020 5: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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am

Re: How to get http response text

Postby Enrico Maria Giordano » Sun Oct 11, 2020 6: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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to get http response text

Postby lorenzoazz » Sun Oct 11, 2020 9: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
lorenzoazz
 
Posts: 37
Joined: Mon Jun 29, 2015 7:41 am


Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 97 guests