Open2
MQL4でPOST
//+------------------------------------------------------------------+
//| trade_history_up.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define INTERNET_FLAG_KEEP_CONNECTION 0x00400000
#define INTERNET_FLAG_RELOAD 0x80000000
#define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100
// 変数宣言
string filename = "TradeHistory.csv";
int handle;
// 関数宣言
void PrepareData();
void SendData(string data);
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
PrepareData();
string data = "user_id=12345&order_ticket=54321&order_close_time=202312150000";
SendData(data);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Prepare data for sending |
//+------------------------------------------------------------------+
void PrepareData()
{
// CSVファイルを開く(存在しない場合は作成)
handle = FileOpen(filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');
if(handle == INVALID_HANDLE)
{
Print("ファイルを開けませんでした。");
return;
}
// CSVに書き込むデータの準備
string data = "user_id, order_ticket, order_close_time\n";
FileWriteString(handle, data);
// データを追加する
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol())
{
data = StringFormat("%s, %d, %d\n", OrderComment(), OrderTicket(), OrderCloseTime());
FileWriteString(handle, data);
}
}
// ファイルを閉じる
FileClose(handle);
}
#import "wininet.dll"
int InternetOpenW(string &sAgent, int lAccessType, string &sProxyName, string &sProxyBypass, int lFlags);
int InternetConnectW(int hInternet, string &szServerName, int nServerPort, string &lpszUsername, string &lpszPassword, int dwService, int dwFlags, int dwContext);
int HttpOpenRequestW(int hConnect, string &Verb, string &ObjectName, string &Version, string &Referer, string &AcceptTypes, uint dwFlags, int dwContext);
bool HttpSendRequestW(int hRequest, string &lpszHeaders, int dwHeadersLength, uchar &lpOptional[], int dwOptionalLength);
int HttpQueryInfoW(int hRequest, int dwInfoLevel, int &lpvBuffer[], int &lpdwBufferLength, int &lpdwIndex);
int InternetReadFile(int hFile, uchar &sBuffer[], int lNumBytesToRead, int &lNumberOfBytesRead);
int InternetCloseHandle(int hInet);
#import
//+------------------------------------------------------------------+
//| Send data through POST request |
//+------------------------------------------------------------------+
void SendData(string data)
{
string userAgent = "MQL4 Agent";
string nullStr = NULL; // Use NULL for empty string parameters
int hInternet = InternetOpenW(userAgent, 1, nullStr, nullStr, 0);
if (hInternet <= 0)
{
Print("Failed to open internet handle: ", GetLastError());
return;
}
string serverName = "example.com";
int hConnect = InternetConnectW(hInternet, serverName, 80, nullStr, nullStr, 3, 0, 1);
if (hConnect <= 0)
{
Print("Failed to connect: ", GetLastError());
InternetCloseHandle(hInternet);
return;
}
string verb = "POST";
string objectName = "/api/data";
string version = "HTTP/1.1";
uint flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE;
int hRequest = HttpOpenRequestW(hConnect, verb, objectName, version, nullStr, nullStr, flags, 1);
if (hRequest <= 0)
{
Print("Failed to open HTTP request: ", GetLastError());
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return;
}
string headers = "Content-Type: application/x-www-form-urlencoded";
uchar dataArr[]; // Define an array to hold bytes
StringToCharArray(data, dataArr); // Convert string data to a uchar array
if (!HttpSendRequestW(hRequest, headers, StringLen(headers), dataArr, ArraySize(dataArr) - 1))
{
Print("Failed to send HTTP request: ", GetLastError());
}
else
{
Print("Data has been successfully sent.");
}
// Close all handles
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
}
//+------------------------------------------------------------------+
//| trade_history_up.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define INTERNET_FLAG_KEEP_CONNECTION 0x00400000
#define INTERNET_FLAG_RELOAD 0x80000000
#define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100
input string user_input_id = ""; // ユーザー入力可能なuser_id
string user_id_file = "LastUserID.txt"; // user_idを保存するファイル名
string last_user_id = ""; // 最後に保存したuser_idを記憶
string last_order_close_time_file = "LastOrderCloseTime.txt"; // 最後のorder_close_timeを保存するファイル名
// 関数宣言
void PrepareData();
void SendData(string data);
bool ShouldSendAllData(string current_user_id, datetime &last_order_close_time);
void SaveLastData(string user_id, datetime last_order_close_time);
void LoadLastData(string &user_id, datetime &last_order_close_time);
#import "wininet.dll"
int InternetOpenW(string &sAgent, int lAccessType, string &sProxyName, string &sProxyBypass, int lFlags);
int InternetConnectW(int hInternet, string &szServerName, int nServerPort, string &lpszUsername, string &lpszPassword, int dwService, int dwFlags, int dwContext);
int HttpOpenRequestW(int hConnect, string &Verb, string &ObjectName, string &Version, string &Referer, string &AcceptTypes, uint dwFlags, int dwContext);
bool HttpSendRequestW(int hRequest, string &lpszHeaders, int dwHeadersLength, uchar &lpOptional[], int dwOptionalLength);
int InternetReadFile(int hFile, uchar &sBuffer[], int lNumBytesToRead, int &lNumberOfBytesRead);
int InternetCloseHandle(int hInet);
#import
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
datetime last_known_order_close_time;
LoadLastData(last_user_id, last_known_order_close_time);
if (ShouldSendAllData(user_input_id, last_known_order_close_time)) {
PrepareData(last_known_order_close_time); // 異なるuser_idがある場合または時間後のデータを送信
}
string data = "user_id=" + user_input_id + "&order_ticket=54321&order_close_time=202312150000";
SendData(data);
SaveLastData(user_input_id, last_known_order_close_time); // 現在のuser_idとorder_close_timeをファイルに保存
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Prepare data for sending |
//+------------------------------------------------------------------+
void PrepareData(datetime from_time)
{
// CSVファイルを開く(存在しない場合は作成)
int handle = FileOpen("TradeHistory.csv", FILE_CSV|FILE_READ|FILE_WRITE, ',');
if(handle == INVALID_HANDLE)
{
Print("ファイルを開けませんでした。");
return;
}
// CSVに書き込むデータの準備
string data = "user_id, order_ticket, order_close_time\n";
FileWriteString(handle, data);
// データを追加する
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
datetime order_close_time = OrderCloseTime();
if(order_close_time > from_time)
{
data = StringFormat("%s, %d, %d\n", OrderComment(), OrderTicket(), order_close_time);
FileWriteString(handle, data);
}
}
}
// ファイルを閉じる
FileClose(handle);
}
//+------------------------------------------------------------------+
//| Determine if all data should be sent based on user_id changes |
//+------------------------------------------------------------------+
bool ShouldSendAllData(string current_user_id, datetime &last_order_close_time)
{
return (current_user_id != last_user_id || TimeCurrent() > last_order_close_time);
}
//+------------------------------------------------------------------+
//| Save current user_id and last_order_close_time to file |
//+------------------------------------------------------------------+
void SaveLastData(string user_id, datetime last_order_close_time)
{
FileDelete(user_id_file); // Remove old file
FileDelete(last_order_close_time_file); // Remove old file
int file_handle = FileOpen(user_id_file, FILE_WRITE|FILE_TXT);
if(file_handle != INVALID_HANDLE)
{
FileWriteString(file_handle, user_id);
FileClose(file_handle);
}
else
{
Print("Failed to save user_id to file.");
}
file_handle = FileOpen(last_order_close_time_file, FILE_WRITE|FILE_TXT);
if(file_handle != INVALID_HANDLE)
{
FileWriteString(file_handle, IntegerToString(last_order_close_time));
FileClose(file_handle);
}
else
{
Print("Failed to save last_order_close_time to file.");
}
}
//+------------------------------------------------------------------+
//| Load last user_id and last_order_close_time from file |
//+------------------------------------------------------------------+
void LoadLastData(string &user_id, datetime &last_order_close_time)
{
if (FileIsExist(user_id_file))
{
int file_handle = FileOpen(user_id_file, FILE_READ|FILE_TXT);
if(file_handle != INVALID_HANDLE)
{
user_id = FileReadString(file_handle);
FileClose(file_handle);
}
}
if (FileIsExist(last_order_close_time_file))
{
int file_handle = FileOpen(last_order_close_time_file, FILE_READ|FILE_TXT);
if(file_handle != INVALID_HANDLE)
{
last_order_close_time = StrToTime(FileReadString(file_handle));
FileClose(file_handle);
}
}
}
#import "wininet.dll"
int InternetOpenW(string &sAgent, int lAccessType, string &sProxyName, string &sProxyBypass, int lFlags);
int InternetConnectW(int hInternet, string &szServerName, int nServerPort, string &lpszUsername, string &lpszPassword, int dwService, int dwFlags, int dwContext);
int HttpOpenRequestW(int hConnect, string &Verb, string &ObjectName, string &Version, string &Referer, string &AcceptTypes, uint dwFlags, int dwContext);
bool HttpSendRequestW(int hRequest, string &lpszHeaders, int dwHeadersLength, uchar &lpOptional[], int dwOptionalLength);
int HttpQueryInfoW(int hRequest, int dwInfoLevel, int &lpvBuffer[], int &lpdwBufferLength, int &lpdwIndex);
int InternetReadFile(int hFile, uchar &sBuffer[], int lNumBytesToRead, int &lNumberOfBytesRead);
int InternetCloseHandle(int hInet);
#import
//+------------------------------------------------------------------+
//| Send data through POST request |
//+------------------------------------------------------------------+
void SendData(string data)
{
string userAgent = "MQL4 Agent";
string nullStr = NULL; // Use NULL for empty string parameters
int hInternet = InternetOpenW(userAgent, 1, nullStr, nullStr, 0);
if (hInternet <= 0)
{
Print("Failed to open internet handle: ", GetLastError());
return;
}
string serverName = "example.com";
int hConnect = InternetConnectW(hInternet, serverName, 80, nullStr, nullStr, 3, 0, 1);
if (hConnect <= 0)
{
Print("Failed to connect: ", GetLastError());
InternetCloseHandle(hInternet);
return;
}
string verb = "POST";
string objectName = "/api/data";
string version = "HTTP/1.1";
uint flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE;
int hRequest = HttpOpenRequestW(hConnect, verb, objectName, version, nullStr, nullStr, flags, 1);
if (hRequest <= 0)
{
Print("Failed to open HTTP request: ", GetLastError());
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return;
}
string headers = "Content-Type: application/x-www-form-urlencoded";
uchar dataArr[]; // Define an array to hold bytes
StringToCharArray(data, dataArr); // Convert string data to a uchar array
if (!HttpSendRequestW(hRequest, headers, StringLen(headers), dataArr, ArraySize(dataArr) - 1))
{
Print("Failed to send HTTP request: ", GetLastError());
}
else
{
Print("Data has been successfully sent.");
}
// Close all handles
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
}