HttpPost.mqh
- //+------------------------------------------------------------------+
- //| HttpPost.mqh |
- //| Copyright 2023, MetaQuotes Ltd. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2023, MetaQuotes Ltd."
- #property link "https://www.mql5.com"
- #property strict
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- class MqlPOST
- {
- public:
- MqlPOST();
- ~MqlPOST();
- string save_order_get_msg(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0);
- string get_msg(string uRL, long account,string key= "NULL",long othernumber= 0);
- string get_last_updated_time(string uRL, long account,string key= "NULL",long othernumber= 0);
- string loginout(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0);
- string save_state(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0);
- string make_web_request(string uRL, int type, long account = 0, string mess = "NULL", string key = "NULL",long othernumber= 0);
-
- };
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void MqlPOST::MqlPOST()
- {
-
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void MqlPOST::~MqlPOST()
- {
-
- }
- //+------------------------------------------------------------------+
- string MqlPOST::save_order_get_msg(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0)
- {
- return make_web_request(uRL, 1, -1, mess,key,othernumber);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MqlPOST::get_msg(string uRL, long account,string key= "NULL",long othernumber= 0)
- {
- return make_web_request(uRL, 2, account, "NULL",key,othernumber);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MqlPOST::get_last_updated_time(string uRL, long account,string key= "NULL",long othernumber= 0)
- {
- return make_web_request(uRL, 3, account, "NULL",key,othernumber);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MqlPOST::loginout(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0)
- {
- return make_web_request(uRL, 5, -1, mess,key,othernumber);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MqlPOST::save_state(string uRL, string mess = "NULL",string key= "NULL",long othernumber= 0)
- {
- return make_web_request(uRL, 4, -1, mess,key,othernumber);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string MqlPOST::make_web_request(string uRL, int type, long account = 0, string mess = "NULL", string key = "NULL",long othernumber= 0)
- {
- // 如果account未提供,使用当前登录账户
- if(account ==0)
- return "";
- if(account < 0)
- account = AccountInfoInteger(ACCOUNT_LOGIN);
-
- string data = "accnumber=" + IntegerToString(account) +
- "&othernumber=" + IntegerToString(othernumber) +
- "&acctype=" + (TerminalInfoInteger(TERMINAL_X64) ? "MT5" : "MT4") +
- "&accbalance=" + DoubleToString(AccountInfoDouble(ACCOUNT_BALANCE),2) +
- "&accequity=" + DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2) +
- "&accprofit=" + DoubleToString(AccountInfoDouble(ACCOUNT_PROFIT),2) +
- "&accmarginlevel=" + DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),2)+"%" +
- "&acclasttime=" + TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES|TIME_SECONDS) +
- "&acccompany=" + AccountInfoString(ACCOUNT_COMPANY) +
- "&state=" + "0" +
- "&msg=" + mess +
- "&key=" + key +
- "&type=" + string(type);
-
- string headers = "Content-Type: application/x-www-form-urlencoded\r\n";
- char result[];
- string response = "";
- uchar postData[];
-
- StringToCharArray(data, postData, 0, WHOLE_ARRAY, CP_UTF8);
- int res = WebRequest("POST", uRL, headers, 5000, postData, result, response);
-
- if(res == 200)
- {
- string resultStr = CharArrayToString(result, 0, -1, CP_UTF8);
- return resultStr;
- }
- else
- {
- printf(uRL + " " + data);
- Print("Web请求失败,错误代码:", res);
- }
- return "";
- }
复制代码
|