MSXML2.ServerXMLHTTP / readyState / Status

MSXML2.ServerXMLHTTP / readyState / Status

Postby Jimmy » Sat Mar 18, 2023 7:04 am

hi,

i like to know where i can find more Information of Value about readyState / Status when using MSXML2.ServerXMLHTTP

Code: Select all  Expand view  RUN
     oHttp:Send()

      nReadystate := oHttp:readyState
      IF nReadystate <> 4  // is there a Constant for 4 ?
         BREAK
      ENDIF

      nStatus := oHttp:Status
      IF nStatus = 200     // this is when got "Response"
         StrFile( oHttp:ResponseBody(), cFile )

but what if 404 or 500 ... how to check it :?:
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Natter » Sat Mar 18, 2023 7:44 am

If I understood your question correctly, then you can check :Status in the loop

Code: Select all  Expand view  RUN
fl:=seconds()
do while .T.
   if (oHttp:readyState=4.and.oHttp:Status=200).or.seconds()-fl>10
      exit
   endif
   millisec(50)
   sysrefresh()
enddo
if oHttp:Status!=200
  msginfo(ltrim(str(oHttp:Status))+CRLF+"Waiting time exceeded")
endif
Natter
 
Posts: 1222
Joined: Mon May 14, 2007 9:49 am

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Jimmy » Sat Mar 18, 2023 9:47 am

hi,

thx for Answer.

my Idea is to give User more Information "why" it fail

my PhpBB Forum "Reader" does not "recognize" when Server is "down"
when using EDGE / Firefox i got

Sorry, that didn’t work.
Please try again or come back later.
500 Error. Internal Server Error.

so how to get "500" to react on it NOT to try to "read" more Message ( in a FOR / NEXT ) from that Forum :?:
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Natter » Sat Mar 18, 2023 10:37 am

Have you tried using WebView ?
Natter
 
Posts: 1222
Joined: Mon May 14, 2007 9:49 am

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Jimmy » Sat Mar 18, 2023 11:14 am

hi,
Natter wrote:Have you tried using WebView ?

yes but it is "too big" to read "only" Message of phpBB Forum
i do not "render" anything, just "GET" Website and "extract" Information to fill local DBF

---

i have search for MSXML2.ServerXMLHTTP and readyState / Status

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms761388(v=vs.85)
readyState Property
(0) UNINITIALIZED The object has been created but has not been initialized because the open method has not been called.
(1) LOADING The object has been created but the send method has not been called.
(2) LOADED The send method has been called and the status and headers are available, but the response is not yet available.
(3) INTERACTIVE Some data has been received. You can call responseBody and responseText to get the current partial results.
(4) COMPLETED All the data has been received, and the complete data is available in responseBody and responseText.


https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms767681(v=vs.85)
status Property

100 Continue
101 Switching protocols
200 OK
201 Created
202 Accepted
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
307 Temporary Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Suitable
417 Expectation Failed
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Natter » Sat Mar 18, 2023 11:21 am

Do you want to receive data exactly by HTTP request or are they visible on the page and are they through DOM ?
Natter
 
Posts: 1222
Joined: Mon May 14, 2007 9:49 am

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Jimmy » Sat Mar 18, 2023 12:41 pm

hi,
Natter wrote:Do you want to receive data exactly by HTTP request or are they visible on the page and are they through DOM ?

no, it is "very simple" just "GET" hole HTML e.g. from this Message, and find "Marker" to "extract" Text
i´m a Internet Technique Dummy ... i once wrote something for Xbase++ to get PDR from Alaska Website

here in Fivewin Forum i have read about a "Reader" which can "extract" CODE TAG from Forum Message
but it work not for me so i wrote my Version and found out that it also work with other Forum which use phpBB

look at this Thread in HMG Forum where you can find Source
https://www.hmgforum.com/viewtopic.php?t=7281

---

as HMG Forum is Online again i can´t "Test" any more ...
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby karinha » Sat Mar 18, 2023 2:13 pm

Code: Select all  Expand view  RUN

// C:\FWH..\SAMPLES\RECEITA2.PRG

#include "FiveWin.ch"

FUNCTION Main()

   LOCAL cOnde, cCnpj, cNome, cEndereco, cNro, cComplemento, cBairro, cCidade, ;
         cEstado, cCep, cTelefone

   cOnde        := SPACE(10)
   cCnpj        := "42905527000105"  // example.
   cNome        := SPACE(50)
   cEndereco    := SPACE(40)
   cNro         := SPACE(10)
   cComplemento := SPACE(10)
   cBairro      := SPACE(20)
   cCidade      := SPACE(30)
   cEstado      := SPACE(02)
   cCep         := SPACE(08)
   cTelefone    := SPACE(17)

   GetCNPJ( cOnde, cCnpj, cNome, cEndereco, cNro, cComplemento, cBairro, cCidade, cEstado, cCep, cTelefone )

RETURN NIL

FUNCTION GetCNPJ( cOnde, cCnpj, cNome, cEndereco, cNro, cComplemento, cBairro, cCidade, cEstado, cCep, cTelefone )

   LOCAL oServerWS, cUrl, aArray, x1, erro, oADODBStream

   IF Len( AllTrim( cCnpj ) ) < 14

      MsgInfo( "Somente Pessoa Juridica para consultar os dados na Receita !!!", "Atenção " )

      RETURN( .T. )

   ENDIF

   cUrl := "https://www.receitaws.com.br/v1/cnpj/" + cCnpj

   #ifdef __XHARBOUR__  // PERFEITO COM XHARBOUR( I Like )

      Try

         Try
            oServerWS := CreateObject( 'MSXML2.XMLHTTP' )
         Catch
            oServerWS := CreateObject( 'Microsoft.XMLHTTP' )
         End

      Catch

         MsgInfo( 'Erro na Criação do Serviço' )

         RETURN NIL

      End

   #else // PERFEITO COM HARBOUR. ( no Like. kkkkk )

      Try

         Try
            oServerWS := win_OleCreateObject( 'MSXML2.XMLHTTP' )
         Catch
            oServerWS := win_OleCreateObject( 'Microsoft.XMLHTTP' )
         End

      Catch

         MsgInfo( 'Erro na Criação do Serviço! Com Harbour', 'Atenção!' )

         RETURN NIL

      End

   #endif

   oServerWS:Open( 'GET', cUrl, .F. )
   oServerWS:setRequestHeader( "Content-Type", "text/xml; charset=utf-8" )
   oServerWS:Send()

   IF oServerWS:STATUS != 200

      MsgStop( AllTrim( Str( oServerWS:Status ) ) + " - " + oServerWS:StatusText, "Erro" )

      RETURN NIL

   ENDIF

   WHILE oServerWS:readyState != 4

      SYSREFRESH()

      // oServerWS:WaitForResponse( 1000 ) // or:

      SysWait( .5 )

   END

   x1 := hb_jsonDecode( oServerWS:responseText, @aArray )

   IF aArray == nil

      MsgStop( "Erro ao retornar os dados. Tente novamente." )

      RETURN NIL

   ENDIF

   IF aArray[ 'status' ] != 'OK'

      MsgStop( aArray[ 'message' ], "Erro" )

      RETURN NIL

   ENDIF

   MsgInfo( "NOME:  " + AllTrim( aArray[ 'nome' ] ) + CRLF + ;
      "FANTASIA:  " + AllTrim( aArray[ 'fantasia' ] ) + CRLF + ;
      "ENDEREÇO:  " + AllTrim( aArray[ 'logradouro' ] ) + ", " + AllTrim( aArray[ 'numero' ] ) + " " + AllTrim( aArray[ 'complemento' ] ) + CRLF + ;
      "BAIRRO:  " + AllTrim( aArray[ 'bairro' ] ) + " CEP: " + AllTrim( aArray[ 'cep' ] ) + CRLF + ;
      "CIDADE:  " + AllTrim( aArray[ 'municipio' ] ) + "-" + AllTrim( aArray[ 'uf' ] ) + CRLF + ;
      "TELEFONE:  " + AllTrim( aArray[ 'telefone' ] ) + CRLF + CRLF + ;
      "NATUREZA JURÍDICA:  " + AllTrim( aArray[ 'natureza_juridica' ] ) + CRLF + ;
      "TIPO:  " + AllTrim( aArray[ 'tipo' ] ) + CRLF + ;
      "SITUAÇÃO:  " + AllTrim( aArray[ 'situacao' ] ) + CRLF + ;
      "DATA SITUAÇÃO:  " + AllTrim( aArray[ 'data_situacao' ] ) + CRLF + ;
      "ULTIMA ATUALIZAÇÃO:  " + AllTrim( aArray[ 'ultima_atualizacao' ] ), "CNPJ CONSULTA" )

   cNome := AllTrim( aArray[ 'nome' ] )
   cEndereco := AllTrim( aArray[ 'logradouro' ] )
   cNro := AllTrim( aArray[ 'numero' ] )
   cNro := StrTran( cNro, ".", "", 1, 10 )
   cNro := Val( cNro )
   cComplemento := AllTrim( aArray[ 'complemento' ] )
   cBairro := AllTrim( aArray[ 'bairro' ] )
   cCep := AllTrim( aArray[ 'cep' ] )
   cCidade := AllTrim( aArray[ 'municipio' ] )
   cEstado := AllTrim( aArray[ 'uf' ] )
   cTelefone  := AllTrim( aArray[ 'telefone' ] )

   IF cOnde = "T"

      cEndereco := cEndereco + " " + AllTrim( Str( cNro, 10 ) ) + " " + AllTrim( cComplemento )

   ENDIF

   cNome        := cNome + spac( 100 - Len( cNome ) )
   cEndereco    := cEndereco + spac( 100 - Len( cEndereco ) )
   cComplemento := cComplemento + spac( 100 - Len( cComplemento ) )
   cBairro      := cBairro + spac( 100 - Len( cBairro ) )
   cCep         := cCep + spac( 10 - Len( cCep ) )
   cCep         := StrTran( cCep, ".", "", 1, 10 )
   cCidade      := cCidade + spac( 50 - Len( cCidade ) )
   cTelefone    := cTelefone + spac( 20 - Len( cTelefone ) )

RETURN NIL
 


Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7831
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: MSXML2.ServerXMLHTTP / readyState / Status

Postby Jimmy » Sat Mar 18, 2023 2:48 pm

hi,
karinha wrote:
Code: Select all  Expand view  RUN

   x1 := hb_jsonDecode( oServerWS:responseText, @aArray )
 

thx for Sample CODE

i think i will make a Fivewin Version of my phpBB Forum Reader
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 37 guests