Linux服务器上Metatrader 4 WebRequest请求失败排查求助
Ubuntu服务器Wine运行MT4时EA WebRequest无响应的排查与解决
问题背景
在Contabo的Ubuntu Linux服务器上通过Wine运行Metatrader 4,交易机器人执行WebRequest请求时无返回结果,但该机器人在本地设备及朋友设备上运行正常。
环境信息
- Ubuntu Linux
- Contabo服务器
- 通过Wine运行MT4
已尝试操作
- 开启Metatrader 4中所有WebRequest相关选项
- 使用ufw解除443端口限制
- 参考多篇端口配置相关教程进行设置
不管是自定义的InternetGetFile函数还是MT4内置WebRequest函数均失效,且已将目标URL添加至MT4专家选项卡的允许列表中。
排查与解决方向
1. 检查Wine网络配置
Wine的网络代理或组件可能存在异常,执行以下操作:
- 查看当前Wine代理设置:
wine reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" - 禁用代理(无代理需求时):
wine reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f - 重置Winetricks网络组件:
winetricks resetwininet
2. 验证服务器网络连通性
直接在服务器终端测试目标URL的网络通路,排除底层网络问题:
- 用curl测试GET请求:
curl -v https://你的目标URL,确认是否能正常返回内容 - 检查DNS解析:
nslookup 你的目标域名,确保域名能正确解析到IP - 测试端口连通性:
nc -zv 目标IP 443,确认443端口可正常访问
3. MT4权限与日志排查
- 确认MT4
工具->选项->EA交易中,已勾选允许WebRequest用于列出的URL,且目标URL完整(需包含https://或http://前缀) - 开启MT4详细日志:
工具->选项->日志,勾选专家系统和网络相关项,执行请求后查看MT4安装目录logs文件夹下的日志,定位具体错误信息
4. Wine版本与MT4兼容性
- 查看当前Wine版本:
wine --version,建议使用稳定版(如Wine 8.x),避免开发版兼容性问题 - 用干净的Wine前缀重新安装MT4:
- 备份现有MT4数据
- 创建新前缀:
WINEPREFIX=~/.wine-mt4 winecfg - 在新前缀下重装MT4并测试EA
5. 自定义函数的Wininet调用修正
自定义InternetGetFile函数使用Wininet库,Wine环境中参数传递可能存在问题:
- 调整
InternetReadFile的参数传递,将cnt改为指针传递(原代码直接传cnt可能导致读取异常):
while(Wininet::InternetReadFile(httprequest,ch,512,&cnt))
- 简化
InternetOpenUrlW的标志位,尝试将16|10改为0测试
6. 服务器安全组检查
除了ufw,Contabo控制面板的安全组也需确认:
- 放行出站的80/443端口
- 无其他限制出站请求的安全规则
用户提供的代码示例
自定义InternetGetFile函数
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); }
MT4内置WebRequest函数代码
//--- 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;
内容的提问来源于stack exchange,提问作者Eqan Ahmad
相关产品推荐
相关产品推荐

