//+------------------------------------------------------------------+ //| 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 "Add Address to the list of allowed ones in the terminal settings to let the example work" #property script_show_inputs input string Address="www.mql5.com"; input int Port =80; bool ExtTLS =false; //+------------------------------------------------------------------+ //| サーバにコマンドを送信 | //+------------------------------------------------------------------+ bool HTTPSend(int socket,string request) { char req[]; int len=StringToCharArray(request,req)-1; if(len<0) return(false); //--- 安全なTLS接続がポート443を使用する場合 if(ExtTLS) return(SocketTlsSend(socket,req,len)==len); //--- 標準TCP接続が使用されている場合 return(SocketSend(socket,req,len)==len); } //+------------------------------------------------------------------+ //| サーバ返答を読む | //+------------------------------------------------------------------+ bool HTTPRecv(int socket,uint timeout) { char rsp[]; string result; uint timeout_check=GetTickCount()+timeout; //--- ソケットからデータが存在する間タイムアウトまで読み取る do { uint len=SocketIsReadable(socket); if(len) { int rsp_len; //--- 接続が安全かどうかに応じたさまざまな読み取りコマンド if(ExtTLS) rsp_len=SocketTlsRead(socket,rsp,len); else rsp_len=SocketRead(socket,rsp,len,timeout); //--- 応答を分析する if(rsp_len>0) { result+=CharArrayToString(rsp,0,rsp_len); //--- 応答ヘッダーのみを印刷する int header_end=StringFind(result,"\r\n\r\n"); if(header_end>0) { Print("HTTP answer header received:"); Print(StringSubstr(result,0,header_end)); return(true); } } } } while(GetTickCount()<timeout_check && !IsStopped()); return(false); } //+------------------------------------------------------------------+ //| スクリプトプログラム開始関数 | //+------------------------------------------------------------------+ void OnStart() { int socket=SocketCreate(); //--- ハンドルを確認する if(socket!=INVALID_HANDLE) { //--- すべて正常な場合は接続する if(SocketConnect(socket,Address,Port,1000)) { Print("Established connection to ",Address,":",Port); 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); ExtTLS=true; } //--- サーバにGET要求を送信する if(HTTPSend(socket,"GET / HTTP/1.1\r\nHost: www.mql5.com\r\nUser-Agent: MT5\r\n\r\n")) { Print("GET request sent"); //--- 応答を読む if(!HTTPRecv(socket,1000)) Print("Failed to get a response, error ",GetLastError()); } else Print("Failed to send GET request, error ",GetLastError()); } else { Print("Connection to ",Address,":",Port," failed, error ",GetLastError()); } //--- 使用後にソケットを閉じる SocketClose(socket); } else Print("Failed to create a socket, error ",GetLastError()); } //+------------------------------------------------------------------+ |