SocketTlsReadAvailable

Lê todos os dados disponíveis a partir de uma conexão TLS segura.

int  SocketTlsReadAvailable(
   int           socket,               // soquete
   uchar&        buffer[],             // buffer para ler dados do soquete
   const uint    buffer_maxlen         // número de bytes necessário para leitura
   );

Parâmetros

socket

[in]  Identificador do soquete retornado pela função SocketCreate. Ao passar um identificador inválido para _LastError é registrado o erro 5270 (ERR_NETSOCKET_INVALIDHANDLE).

buffer

[out]  Referência para array do tipo uchar em que serão lidos os dados. O tamanho da matriz dinâmica é aumentado pelo número de bytes lidos. O tamanho da matriz não pode exceder INT_MAX (2147483647).

buffer_maxlen

[in]  Número de bytes que devem ser lidos no array buffer[]. Os dados que não se encaixam na matriz permanecerão no soquete. Eles estarão disponíveis chamando SocketTlsReadAvailable ou SocketTlsRead. O valor Buffer_maxlen não pode exceder INT_MAX (2147483647).

Valor retornado

Se bem-sucedido, retorna o número de bytes lidos, em caso de erro, retorna -1.

Observação

Se ocorrer um erro durante a execução desta função no soquete do sistema, a conexão estabelecida Socketconnect será interrompida.

Se houver um erro ao ler os dados em _LastError é registrado o erro 5273 (ERR_NETSOCKET_IO_ERROR).

A função só pode ser chamada por EAs e scripts, pois eles trabalham em seu próprio fluxo de execução. Quando chamado do indicador GetLastError() retorna o erro 4014 — "Função do sistema não permitida para chamada".

Exemplo:

//+------------------------------------------------------------------+
//|                                       SocketTlsReadAvailable.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com
#property version     "1.00"
#property script_show_inputs
//+------------------------------------------------------------------+
//| Parâmetros de entrada do script                                  |
//+------------------------------------------------------------------+
input string InpMethod ="GET";            // Method (HEAD,GET)
input string InpServer ="www.google.com"// Server
input uint   InpPort   =443;              // Port
input uint   InpTimeout=5000;             // Timeouts
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(void)
  {
   Print("Server: ",InpServer);
   Print("Port: ",InpPort);
//--- criamos um soquete e obtemos seu identificador
   const int socket=SocketCreate();
 
   if(socket==INVALID_HANDLE)
     {
      Print("SocketCreate() failed. Error ",GetLastError());
      return;
     }
//--- definimos tempos limite para receber e enviar dados para o objeto do sistema de soquete
   if(!SocketTimeouts(socket,InpTimeout,InpTimeout))
     {
      PrintFormat("SocketTimeouts(%u, %u) failed. Error %d",InpTimeout,InpTimeout,GetLastError());
      SocketClose(socket);
      return;
     }
//--- contectamo-nos ao servidor Server na porta Port
   if(!SocketConnect(socket,InpServer,InpPort,InpTimeout))
     {
      PrintFormat("SocketConnect('%s', %u, %u) failed. Error %d",InpServer,InpPort,InpTimeout,GetLastError());
      SocketClose(socket);
      return;
     }
//--- obtemos dados sobre o certificado usado para proteger a conexão de rede
   string   subject,issuer,serial,thumbprint;
   datetime expiration;
 
   if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration))
     {
      Print("TLS certificate:");
      Print("   Owner:      ",subject);
      Print("   Issuer:     ",issuer);
      Print("   Number:     ",serial);
      Print("   Print:      ",thumbprint);
      Print("   Expiration: ",expiration);
     }
   else
     {
      //--- o servidor não fornecer um certificado, informamos que a conexão não é segura e saímos
      Print("The connection is not secured by a certificate");
      SocketClose(socket);
      return;
     }
//--- enviamos a consulta ao servidor
   string request=StringFormat("%s / HTTP/1.1\r\nHost: %s\r\nUser-Agent: MetaTrader 5\r\n\r\n",InpMethod,InpServer);
 
   if(HTTPSendTLS(socket,request))
     {
      //--- consulta enviada, resposta recebida
      Print("\nRequest sent. Starting page loading...");
      uchar response[]; // todos os dados recebidos (cabeçalho e corpo do documento)
 
      if(!HTTPRecvTLS(socket,response,InpTimeout))
        {
         Print("There were errors while reading the page");
         SocketClose(socket);
         return;
        }
      //--- informamos o número de bytes de dados recebidos
      PrintFormat("%u bytes received",response.Size());
      //--- imprimimos somente o cabeçalho da página resultante
      string result    =CharArrayToString(response,0,WHOLE_ARRAY,CP_UTF8);
      int    header_end=StringFind(result,"\r\n\r\n");
 
      if(header_end>0)
        {
         Print("\nHTTP answer header received:");
         Print(StringSubstr(result,0,header_end));
        }
     }
//--- fechamos o soquete após usá-lo
   SocketClose(socket);
   /*
   Resultado:
   Serverwww.google.com
   Port443
   TLS certificate:
      Owner: /CN=www.google.com
      Issuer: /C=US/O=Google Trust Services/CN=WR2
      Number0d:43:b1:4a:bb:9c:15:96:10:e1:3d:55:23:9f:25:4e
      Print89167618e5017f813aff981c88ce422dc1016bdf
      Expiration2024.12.30 08:26:35
 
   Request sentStarting page loading...
   HTTPRecvTLSDocument received within 27 attempts
   25185 bytes received
 
   HTTP answer header received:
   HTTP/1.1 200 OK
   DateFri25 Oct 2024 17:12:42 GMT
   Expires: -1
   Cache-Controlprivatemax-age=0
   Content-Typetext/htmlcharset=ISO-8859-1
   Content-Security-Policy-Report-Onlyobject-src 'none';base-uri 'self';script-src 'nonce-CUL2rdUOeAN7xIV6v0WUuQ' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inlinehttpshttp:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
   Accept-CHSec-CH-Prefers-Color-Scheme
   P3PCP="This is not a P3P policy! See g.co/p3phelp for more info."
   Servergws
   X-XSS-Protection0
   X-Frame-OptionsSAMEORIGIN
   Set-CookieAEC=AVYB7coyYMCdweTDTaWeGYzmRnxzKGqsOEosH_VkbCn8xhWkFz6v0kxQFwexpires=Wed23-Apr-2025 17:12:42 GMTpath=/; domain=.google.comSecureHttpOnlySameSite=lax
   Set-CookieNID=518=J02X02Ff4v_9sMcNoUz-1SolmuG08E26Gs438ik0J_SOJUMy7of-P-qup-LaNSWVXUL8OjhOXpGIGuJQGIoEPBnzqDKCH-46_FN4J2foHeWTlGG8bVVvQ44AHWLg1OXjrGp3CUBexYdczLWNy3LxEcb7eh6mxSvFzOelPC6-vpXkaumLQ80x9gF_RpLcAYfN4ehTexpires=Sat26-Apr-2025 17:12:42 GMTpath=/; domain=.google.comHttpOnly
   Alt-Svch3=":443"ma=2592000,h3-29=":443"ma=2592000
   Accept-Rangesnone
   VaryAccept-Encoding
   Transfer-Encodingchunked
   */
  }
//+------------------------------------------------------------------+
//| Enviamos uma solicitação HTTP por meio de uma conexão segura     |
//+------------------------------------------------------------------+
bool HTTPSendTLS(int socket,const string request)
  {
//--- convertemos a string em um array de caracteres, descartando o zero final
   char req[];
   int  len=StringToCharArray(request,req,0,WHOLE_ARRAY,CP_UTF8)-1;
 
   if(len<0)
      return false;
 
   return(SocketTlsSend(socket,req,len)==len);
  }
//+------------------------------------------------------------------+
//| Recebe uma página da Web por meio de uma conexão segura     |
//+------------------------------------------------------------------+
bool HTTPRecvTLS(int socket,uchar &response[],const uint timeout_ms)
  {
//--- lemos os dados disponíveis de uma conexão protegida por TLS antes que o tempo de espera expire
   ulong timeout_check=GetTickCount64()+timeout_ms;
   uchar block[1024];   // buffer de bloco para leitura de dados de um soquete
   uint  attempt=0;     // número solicitado de blocos de dados
   int   err    =0;     // código de erro
 
   ResetLastError();
 
   do
     {
      //--- lemos em blocos de no máximo 1024 bytes
      int len=SocketTlsReadAvailable(socket,block,1024);
 
      if(len>0)
        {
         attempt++;
         //--- juntamos os blocos de dados resultantes
         ArrayCopy(response,block,response.Size());
         //--- aqui analisamos os dados recebidos, determinamos o cabeçalho, o corpo da página, o final ou o erro de carregamento, etc.
         //...
         //...
         //...
         timeout_check=GetTickCount64()+timeout_ms;
        }
      else
         Sleep(10);
 
      err=GetLastError();
     }
   while(!IsStopped() && GetTickCount()<timeout_check && !err);
//--- Houve algum erro na leitura?
   if(err)
     {
      Print("Error ",err);
      return(false);
     }
 
   PrintFormat("%s: Document received within %d attempts",__FUNCTION__,attempt);
   return(true);
  }

Veja também

SocketTimeouts, MathSwap