/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Class Library // ---------------------------------------------- // Name: XSCHttp // Description: provides all HTTP requirements ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// // // Global Properties ... #property library #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // // START Definitions ... // // enum X_HTTP_METHOD { X_HTTP_GET, X_HTTP_POST }; // // Convert enum to String ... string ToString(X_HTTP_METHOD method) { // string result = ""; // switch (method) { // case X_HTTP_GET: result = "GET"; break; // case X_HTTP_POST: result = "POST"; break; } // return result; } // // END Definitions ... // // // START Import and Inclused requirements ... // // // Includes ... #include "../Libraries/x-saherelm.common.lib.mq5" // // END Import and Inclused requirements ... // // // START Overrides ... // // // END Overrides ... // // // a Class for Manage Account ... class XSCHttp { // // Public ... public: // // Constructor ... void XSCHttp() { XSCHttp("", 10000); } void XSCHttp( string path, // Base Folder to Store Data int timeout // base timeout for Requests ) { // Path(path); Timeout(timeout); } // // Deconstructor ... void ~XSCHttp() { } // // START Getter(s)/Setter(s) ... // // // Path ... void Path(string value) { mPath = value; } string Path() { return mPath; } // // Timeout ... void Timeout(int value) { mTimeout = value; } // int Timeout() { return mTimeout; } // // Error ... int Error() { return mError; } // // Response ... string Response() { return mResponse; } // // END Getter(s)/Setter(s) ... // // // START Provided Functions ... // // // Send Global Request ... int SendRequest( X_HTTP_METHOD method, // Httm Request Method const string url, // Server Address const string headers, // Headers providing const char &payload[], // the Data which needs to Send char &response[], // Response of request string responseHeaders, // Response Headers int timeout = 500 // Timeout for response default is 500 ) { // int result = -1; // string strMethod = ToString(method); if (StringLen(strMethod) == 0) { return result; } // // Reset State ... ResetState(); // result = WebRequest( strMethod, url, headers, timeout, payload, response, responseHeaders); // if (result < 0) { mError = GetLastError(); } else { mResponse = CharArrayToString(response); } // return result; } // // Get Request ... int GetRequest( const string url, // Server Address const string headers, // Headers providing const char &payload[], // the Data which needs to Send char &response[], // Response of request string responseHeaders, // Response Headers int timeout = 500 // Timeout for response default is 500 ) { // int result = SendRequest( X_HTTP_GET, url, headers, payload, response, responseHeaders, timeout); // return result; } // // Post Request ... int PostRequest( const string url, // Server Address const string headers, // Headers providing const char &payload[], // the Data which needs to Send char &response[], // Response of request string responseHeaders, // Response Headers int timeout = 500 // Timeout for response default is 500 ) { // int result = SendRequest( X_HTTP_POST, url, headers, payload, response, responseHeaders, timeout); // return result; } // // Handle Download Specific URL Content to Specific Path and File Name ... bool Download( string url, // the URL address which going to download string fileName // Specify Destination file name to Store Response ) { // bool result = false; // string filePath = GetFilePath(fileName); string cookie = NULL; string referer = NULL; int timeout = Timeout(); // char payload[]; string headers; char response[]; string responseHeaders; // // Send Request ... int requestResult = GetRequest( url, headers, payload, response, responseHeaders, timeout); // // Define File Handler ... int mFileHandler = FileOpen( filePath, FILE_WRITE | FILE_BIN); if (mFileHandler == INVALID_HANDLE) { // mError = GetLastError(); return result; } // // Write Response to File ... uint writed = FileWriteArray( mFileHandler, response, 0, ArraySize(response)); FileFlush(mFileHandler); FileClose(mFileHandler); // result = writed > 0; // return result; } // // END Provided Functions ... // // // Protected ... protected: // // Destintion Folder Path ... string mPath; // // WEB Request Timeout Value ... int mTimeout; // // WEB Request's Response ... string mResponse; // // Error Value ... int mError; // // Private ... private: // // Reset Errors State ... void ResetState() { // // Reset Errors ... mError = -1; mResponse = ""; ResetLastError(); } // // Generate Full File Path ... string GetFilePath(string fileName) { // string result = ""; // result = // Path() + "\\" + fileName // ; // return result; } }; // // START Usefull Functions ... // // // END Usefull Functions ... //