373 lines
6.8 KiB
Plaintext
373 lines
6.8 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// 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
|
|
|
|
//
|
|
// Definitions ...
|
|
|
|
//
|
|
enum X_HTTP_METHOD
|
|
{
|
|
X_HTTP_GET,
|
|
X_HTTP_POST
|
|
};
|
|
|
|
//
|
|
// Imports ...
|
|
|
|
//
|
|
// Includes ...
|
|
#include "../Classes/x-saherelm.base.class.mq5"
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// a Class for Manage Account ...
|
|
class XCHttp : public XCBase
|
|
{
|
|
//
|
|
// Public ...
|
|
public:
|
|
//
|
|
// Constructor ...
|
|
void XCHttp()
|
|
{
|
|
XCHttp("", 10000);
|
|
}
|
|
void XCHttp(
|
|
string path, // Base Folder to Store Data
|
|
int timeout // base timeout for Requests
|
|
)
|
|
{
|
|
//
|
|
Path(path);
|
|
Timeout(timeout);
|
|
}
|
|
|
|
//
|
|
// Deconstructor ...
|
|
void ~XCHttp()
|
|
{
|
|
}
|
|
|
|
//
|
|
// Properties Getter(s) / Setter(s) ...
|
|
|
|
//
|
|
// Path ...
|
|
void Path(string value)
|
|
{
|
|
//
|
|
mPath = value;
|
|
|
|
//
|
|
if (!IsValid(mPath))
|
|
{
|
|
mPath = GetTag();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
//
|
|
// Overrides ...
|
|
string GetTag() override
|
|
{
|
|
return GetSpecificToken(this);
|
|
}
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
};
|
|
|
|
//
|
|
// Tools ...
|
|
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
// |