我目前在 Linux 服务器上托管 Metatrader 4,其中 Metatrader 4 上的交易机器人执行 Web 请求。 同一个机器人在我自己的系统和我朋友的系统上运行良好。但在服务器上它没有从网络请求中获取任何内容。 ()
环境信息:
目前已尝试过
以下是网络请求代码,它在我的所有个人系统上运行良好,但不幸的是在 contabo 服务器上运行不正常:
int InternetGetFile(string url,string &content)
{
//--- Init
content=NULL;
//--- Create connection
int httpconnect=0;
int httprequest=0;
int httpopen=Wininet::InternetOpenW("InternetGetFileMQL",0," "," ",0);
int e=kernel32::GetLastError();
if(e==0)
{
bool flag=Wininet::DeleteUrlCacheEntryW(url);
httprequest=Wininet::InternetOpenUrlW(httpopen,url,NULL,0,16|10,0);
e=kernel32::GetLastError();
if(e==0)
{
//--- Define buffers
uchar ch[512];
string temp="";
//--- Retrieve data from file
int cnt=0;
while(Wininet::InternetReadFile(httprequest,ch,512,cnt))
{
//e=kernel32::GetLastError();
if(cnt<=0)
break;
temp=temp+CharArrayToString(ch,0,cnt);
}
//--- Store result
content=temp;
}
}
//--- Close connection
if(httprequest>0)
InternetCloseHandle(httprequest);
if(httpopen>0)
InternetCloseHandle(httpopen);
//--- Get out and return error code
return(e);
}
编辑:
我发现了一些有趣的东西可能会有所帮助:
我替换了之前的代码并尝试使用内置的mql4 Web请求功能
//--- Call WebRequest function
string method = "GET";
string headers = "";
int timeout = 5000; // Timeout in milliseconds (adjust as needed)
char data[]; // No data to send
char result[4096]; // Fixed-size array to hold response (adjust as needed)
string result_headers;
int request = WebRequest(method, url, headers, timeout, data, result, result_headers);
// Check for successful request
if (request == 200) {
// Copy result to content string
content = CharArrayToString(result);
Print("Data collected");
} else {
// Handle error (you may want to add more error handling)
Print("Error: Web request failed with code ", request);
}
return request;
我收到这些错误消息
有趣的是我在 mt4 的专家选项卡中添加了这些 URL
我已经解决了这个问题。发现运行Wine的Windows服务器或Ubuntu服务器不支持Wininet:
所以我想建议面临类似问题的人使用 WebRequest,它在底层使用 WinHTTP,Windows 服务器和 Ubuntu 服务器都支持。