//+------------------------------------------------------------------+ //| SocketExample.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2000-2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property description "Para que el ejemplo funcione, añada Address a la lista de permitidas en los ajustes del terminal" #property script_show_inputs input string Address="www.mql5.com"; input int Port =80; bool ExtTLS =false; //+------------------------------------------------------------------+ //| Enviando comandos al servidor | //+------------------------------------------------------------------+ bool HTTPSend(int socket,string request) { char req[]; int len=StringToCharArray(request,req)-1; if(len<0) return(false); //--- si se usa una conexión TLS protegida a través del puerto 443 if(ExtTLS) return(SocketTlsSend(socket,req,len)==len); //--- si se usa una conexión TCP normal return(SocketSend(socket,req,len)==len); } //+------------------------------------------------------------------+ //| Leyendo la respuesta del servidor | //+------------------------------------------------------------------+ bool HTTPRecv(int socket,uint timeout) { char rsp[]; string result; uint timeout_check=GetTickCount()+timeout; //--- leemos los datos del socket mientras haya, pero no por un tiempo superior a timeout do { uint len=SocketIsReadable(socket); if(len) { int rsp_len; //--- diferentes comandos de lectura, dependiendo de si la conexión está o no protegida if(ExtTLS) rsp_len=SocketTlsRead(socket,rsp,len); else rsp_len=SocketRead(socket,rsp,len,timeout); //--- analizamos la respuesta if(rsp_len>0) { result+=CharArrayToString(rsp,0,rsp_len); //--- imprimimos solo el encabezado de la respuesta int header_end=StringFind(result,"\r\n\r\n"); if(header_end>0) { Print("Obtenido el encabezado HTTP de la respuesta:"); Print(StringSubstr(result,0,header_end)); return(true); } } } } while(GetTickCount()<timeout_check && !IsStopped()); return(false); } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { int socket=SocketCreate(); //--- comprobando el manejador if(socket!=INVALID_HANDLE) { //--- si todo está en orden, nos conectamos if(SocketConnect(socket,Address,Port,1000)) { Print("Conexión establecida con ",Address,":",Port); string subject,issuer,serial,thumbprint; datetime expiration; //--- si la conexión está protegida con un certificado, mostramos sus datos if(SocketTlsCertificate(socket,subject,issuer,serial,thumbprint,expiration)) { Print("Certificado TLS:"); Print(" Propietario: ",subject); Print(" Emisor: ",issuer); Print(" Número: ",serial); Print(" Huella digital: ",thumbprint); Print(" Expiración: ",expiration); ExtTLS=true; } //--- enviamos al servidor una solicitud GET if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n")) { Print("Solicitud GET enviada"); //--- leemos la respuesta if(!HTTPRecv(socket,1000)) Print("No se ha podido obtener la respuesta, error ",GetLastError()); } else Print("No se ha logrado enviar la solicitud GET, error ",GetLastError()); } else { Print("No se ha logrado conectar con ",Address,":",Port," error ",GetLastError()); } //--- cerramos el socket después de usarlo SocketClose(socket); } else Print("No se ha logrado crear el socket, error ",GetLastError()); } //+------------------------------------------------------------------+ |