English Русский Español Deutsch 日本語
preview
Connexus入门(第一部分):如何使用WebRequest函数?

Connexus入门(第一部分):如何使用WebRequest函数?

MetaTrader 5示例 | 28 四月 2025, 13:25
116 0
joaopedrodev
joaopedrodev

概述

在金融编程领域,尤其是在MetaTrader 5的背景下,通过HTTP与远程服务器进行交互的能力至关重要。无论是获取实时市场数据、向API发送交易指令,还是查询第三方服务,HTTP请求都扮演着关键角色。在MQL5中,WebRequest函数是用于此类通信的本地工具,但它的限制使其在许多场景中无法实际应用。

HTTP(超文本传输协议)是网络通信的基础,掌握其使用方法可以让开发人员在MetaTrader 5与其他服务之间创建丰富且动态的集成。例如,一个EA可能需要访问新闻API,根据全球事件调整其交易策略。另一个常见应用场景是,通过那些提供HTTP应用程序接口(API)的交易所来查询加密货币价格,从而使EA能够同步市场行情并进行交易。

尽管HTTP请求很重要,但MQL5中WebRequest函数的实现并不完全符合现代且灵活工具的期望。这给开发人员带来了一个挑战:要么适应这些限制,要么寻找变通方法。我们从现在开始的系列文章旨在实现这一点——探索WebRequest函数的弱点,并构建一个名为Connexus的库,以克服这些限制,使MQL5开发人员的工作更轻松。

WebRequest函数为与外部服务的集成开辟了广泛的可能性。从收集对自动化交易决策至关重要的金融数据,到完全自动化流程,该函数允许EA直接与网络交互。例如,这使得您的EA能够从外部来源(如经济新闻或其他平台的市场数据)获取实时信息。这些数据能够被处理并用于自动调整您的交易策略,从而提高操作的准确性和效率。

然而,正如将在实例中展示的那样,使用WebRequest函数可能并不简单。发送HTTP请求看似简单,但您很快会遇到技术挑战,例如发送正确的头部信息、格式化JSON数据、正确处理服务器响应,甚至处理通信过程中可能出现的错误和异常。这些挑战表明,尽管功能强大,但该函数需要对协议和系统间通信有扎实的理解,这可能会成为刚开始探索这一领域的开发人员的重大障碍。

这正是需要一个更易于使用且高效的工具之处。之后将在本系列后续文章中开发和改进的Connexus库,旨在克服这些限制,使WebRequest的集成过程对用户而言更加友好且直观。有了Connexus,开发人员可以专注于真正重要的事情:他们的应用程序和EA的逻辑,而无需直接处理网络编程底层的技术细节。您将能够专注于高效地集成系统,拥有一个清晰且功能强大的界面,而不是浪费时间调试格式或头部错误。

本系列文章将致力于深入探讨WebRequest函数的弱点、其限制,以及我们在开发强大解决方案时如何解决这些问题。除了继续讨论HTTP协议外,我们还将涵盖诸如API身份验证、处理大量数据以及实现高级功能(如响应时间控制和处理多个同时请求)等方面。

因此,如果您对提升您的MQL5开发技能、了解系统集成以及优化HTTP通信流程感兴趣,请关注后续发布的内容。我们将继续扩展该项目的范围,指导您开发Connexus库,使其成为您开发工具箱中不可或缺的工具。这里获得的知识不仅对MetaTrader的使用者有用,而且对任何需要将API和网络服务集成到其应用程序中的开发人员都有帮助。


了解WebRequest

WebRequest函数是MQL5提供的用于发送HTTP请求的主要工具。简单来说,它允许一个MQL5程序向服务器发送请求并接收响应。尽管看似简单,但实际使用WebRequest会暴露出许多陷阱和复杂性,可能会使开发变得复杂。

该函数的基本语法有两种版本:

int  WebRequest(
   const string      method,           // HTTP method 
   const string      url,              // URL
   const string      cookie,           // cookie
   const string      referer,          // referer
   int               timeout,          // timeout
   const char        &data[],          // the array of the HTTP message body
   int               data_size,        // data[] array size in bytes
   char              &result[],        // an array containing server response data
   string            &result_headers   // headers of server response
   );

int  WebRequest(
   const string      method,           // HTTP method
   const string      url,              // URL
   const string      headers,          // headers 
   int               timeout,          // timeout
   const char        &data[],          // the array of the HTTP message body
   char              &result[],        // an array containing server response data
   string            &result_headers   // headers of server response
   );

WebRequest函数的参数对于其正常运行至关重要,任何希望有效使用它的开发人员都需要对其有深入的了解。让我们来逐一探讨这些参数:

参数名称
类型
输入/ 输出
说明
method
字符串
输入
该参数定义了您想要发起的HTTP请求类型。最常见的类型是GET和POST。GET类型用于从服务器请求数据,而POST类型用于向服务器发送数据。
url
字符串
输入
这是请求将要发送到的服务器的地址。URL必须是完整的,包括协议(http或https)。
headers
字符串
输入
附加的HTTP头部信息可以作为字符串数组传递。每个字符串必须是“键:值”(Key: Value)的格式。这些头部信息用于传递附加信息,例如身份验证令牌或内容类型,每个头部信息之间用换行符“\r\n”分隔。
cookie
字符串
输入
Cookie值。
referer
字符串
输入
HTTP请求中“Referer”头部的值。
timeout
整型 输入
设置等待服务器响应的最大时间,单位为毫秒。合理的超时时间至关重要,以确保EA不会因等待可能永远不会到达的响应而卡死。
data
字符型 输入
对于POST请求,此参数用于将数据发送到服务器。这些数据需要以字节数组的形式发送,对于不熟悉二进制数据处理的开发人员来说,这可能会有些困难。
data_size
整型 输入 将要发送的数据的大小。它必须与数据数组的大小一致,请求才能正确工作。
result
字符型 输出 此参数接收来自服务器的响应,响应同样以字节数组的形式返回。在函数被调用后,需要对数组解码以提取有用的信息。
  result_headers   字符串 输出 该字符串数组用于接收来自服务器的响应头部,其中可能包含重要信息,例如内容类型和身份验证状态。 

设置中的每一项都必须完整配置,以确保能够正确发出请求。这些参数中的任何错误都可能导致请求格式错误或完全无法与服务器通信。

该函数返回一个HTTP状态码,用于指示操作的成功或失败。尽管WebRequest涵盖了基本概念,但其实现还存在很多不足之处。它要求开发人员手动管理头部的创建、不同类型数据的处理以及错误检查,使得整个过程繁琐且容易出错。WebRequest的一个优点是它支持GET和POST请求,这使得它能够与各种API进行交互。



WebRequest函数的实例

为了说明WebRequest函数的使用,我们来构建一个简单的实例。为此,我们将使用httpbin.org,这是一个免费的在线服务,允许您发起和测试HTTP请求。它是由kennethreitz创建的,是一个开源项目(链接)。此服务的工作方式非常简单容易。它基本上是一个“镜子”。您站在镜子前摆姿势或提问。这就类似向HTTP Bin发送一个请求。镜子会精确地反映您正在做的事情。这就类似HTTP Bin接收并反映您发送的内容。对于想要检查HTTP请求中确切发送内容的开发人员,或者需要模拟不同类型的请求和响应的开发人员来说,这是一个很有用的工具。httpbin的一些常见功能包括:

  1. 发送请求:您可以发送不同类型的HTTP请求,例如GET、POST、PUT、DELETE等,以查看服务器的响应。
  2. HTTP头部测试:您可以发送自定义头部并查看服务器的响应,这对于调试与头部相关的问题很有用。
  3. 在请求正文中发送数据:可以测试在请求正文中发送数据,并查看服务器如何处理它。
  4. HTTP状态模拟:您可以请求服务器返回特定的状态码,例如200、404、500等,以测试您的应用程序如何处理不同的状态响应。
  5. 延迟和重定向的模拟:httpbin.org允许模拟响应延迟或重定向,有助于在更复杂的场景中测试系统行为。
  6. Cookie测试:您可以操作Cookie,查看如何在服务器上设置和返回。

这是一个用于集成使用HTTP协议的系统的实用工具。让我们使用WebRequest发起最简单的GET请求。

第一步:在终端中添加URL

根据文档说明:要使用WebRequest()函数,需要将所需的服务器地址添加到“选项”窗口中“工具”选项卡下的允许URL列表中。服务器的端口会根据指定的协议自动选择——对于“http://”是80端口,对于“https://”是443端口。







第二步:手动编写代码

在目录<data_folder>/MQL5/Experts中,创建一个名为Connexus的文件夹。我们将把测试文件放在这个文件夹中。要找到数据文件夹,在MetaTrader或MetaEditor的主菜单中,选择文件 > 打开数据文件夹。在该文件夹内,创建一个名为“WebRequest.mq5”的文件,最终结果大致如下:

//+------------------------------------------------------------------+
//|                                                   WebRequest.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+

目前,我们仅使用OnInit()事件来进行测试。让我们定义一些变量以执行请求,并将它们传递给WebRequest函数。

int OnInit()
  {
//--- Defining variables
   string method = "GET";                    // HTTP verb in string (GET, POST, etc...)
   string url = "https://httpbin.org/get";   // Destination URL
   string headers = "";                      // Request header
   int timeout = 5000;                       // Maximum waiting time 5 seconds
   char data[];                              // Data we will send (body) array of type char
   char result[];                            // Data received as an array of type char
   string result_headers;                    // String with response headers
   
//--- Calling the function and getting the status_code
   int status_code = WebRequest(method,url,headers,timeout,data,result,result_headers);
   
//--- Print the data
   Print("Status code: ",status_code);
   Print("Response: ",CharArrayToString(result)); // We use CharArrayToString to display the response in string form.

//---
   return(INIT_SUCCEEDED);
  }

当我们把EA添加到图表中时,终端会打印出以下响应:

WebRequest (WINV24,M1)  Status code: 200
WebRequest (WINV24,M1)  Resposta: {
WebRequest (WINV24,M1)    "args": {}, 
WebRequest (WINV24,M1)    "headers": {
WebRequest (WINV24,M1)      "Accept": "*/*", 
WebRequest (WINV24,M1)      "Accept-Encoding": "gzip, deflate", 
WebRequest (WINV24,M1)      "Accept-Language": "pt,en;q=0.5", 
WebRequest (WINV24,M1)      "Host": "httpbin.org", 
WebRequest (WINV24,M1)      "User-Agent": "MetaTrader 5 Terminal/5.4476 (Windows NT 11.0.22631; x64)", 
WebRequest (WINV24,M1)      "X-Amzn-Trace-Id": "Root=1-66d8cd53-0d6cd16368aa22e455db461c"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "origin": "XXX.XXX.XX.XXX", 
WebRequest (WINV24,M1)    "url": "https://httpbin.org/get"
WebRequest (WINV24,M1)  }
WebRequest (WINV24,M1)  

请注意,收到的状态码是200,这意味着请求成功了,并且在响应中我们收到了一个包含部分数据的JSON。在下一篇文章中,我们将更详细地探讨通过HTTP协议进行通信的工作原理。现在我们将HTTP动词改为POST,并在请求正文中发送数据。

int OnInit()
  {
//--- Defining variables
   string method = "POST";                    // HTTP verb in string (GET, POST, etc...)
   string url = "https://httpbin.org/post";   // Destination URL
   string headers = "";                       // Request header
   int timeout = 5000;                        // Maximum waiting time 5 seconds
   char data[];                               // Data we will send (body) array of type char
   char result[];                             // Data received as an array of type char
   string result_headers;                     // String with response headers

//--- Treating body
   string body = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
   StringToCharArray(body,data,0,WHOLE_ARRAY,CP_UTF8); // Converts a string to a byte array

//--- Calling the function and getting the status_code
   int status_code = WebRequest(method,url,headers,timeout,data,result,result_headers);
   
//--- Print the data
   Print("Status code: ",status_code);
   Print("Response: ",CharArrayToString(result)); // We use CharArrayToString to display the response in string form.
   
//---
   return(INIT_SUCCEEDED);
  }

好的,现在一切正常。考虑到httpbin会返回我们发送的所有内容,那么其应该会返回发送的正文,对吧?执行POST代码后返回:

WebRequest (WINV24,M1)  Status code: 200
WebRequest (WINV24,M1)  Resposta: {
WebRequest (WINV24,M1)    "args": {}, 
WebRequest (WINV24,M1)    "data": "", 
WebRequest (WINV24,M1)    "files": {}, 
WebRequest (WINV24,M1)    "form": {
WebRequest (WINV24,M1)      "{\"key1\":\"value1\",\"key2\":\"value2\"}\u0000": ""
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "headers": {
WebRequest (WINV24,M1)      "Accept": "*/*", 
WebRequest (WINV24,M1)      "Accept-Encoding": "gzip, deflate", 
WebRequest (WINV24,M1)      "Accept-Language": "pt,en;q=0.5", 
WebRequest (WINV24,M1)      "Content-Length": "34", 
WebRequest (WINV24,M1)      "Content-Type": "application/x-www-form-urlencoded", 
WebRequest (WINV24,M1)      "Host": "httpbin.org", 
WebRequest (WINV24,M1)      "User-Agent": "MetaTrader 5 Terminal/5.4476 (Windows NT 11.0.22631; x64)", 
WebRequest (WINV24,M1)      "X-Amzn-Trace-Id": "Root=1-66d9bc77-314c004a607c383b3197c15a"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "json": null, 
WebRequest (WINV24,M1)    "origin": "200.103.20.126", 
WebRequest (WINV24,M1)    "url": "https://httpbin.org/post"
WebRequest (WINV24,M1)  }
WebRequest (WINV24,M1)  

请注意,我们有一些附加的字段,例如“json”和“data”。让我们来理解每一个字段。“data”字段应该显示我们以字符串格式发送的正文,而“json”字段应该显示我们以JSON格式发送的正文。但是,如果我们已经在请求中发送了正文,为什么这两个字段都为空呢?因为我们必须告知服务器,内容类型将是JSON。为此,我们需要配置请求头部,如下所示:

int OnInit()
  {
//--- Defining variables
   string method = "POST";                    // HTTP verb in string (GET, POST, etc...)
   string url = "https://httpbin.org/post";   // Destination URL
   string headers = "Content-Type: application/json";// Request header
   int timeout = 5000;                        // Maximum waiting time 5 seconds
   char data[];                               // Data we will send (body) array of type char
   char result[];                             // Data received as an array of type char
   string result_headers;                     // String with response headers

//--- Treating body
   string body = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
   StringToCharArray(body,data,0,WHOLE_ARRAY,CP_UTF8);

//--- Calling the function and getting the status_code
   int status_code = WebRequest(method,url,headers,timeout,data,result,result_headers);
   
//--- Print the data
   Print("Status code: ",status_code);
   Print("Response: ",CharArrayToString(result)); // We use CharArrayToString to display the response in string form.
   
//---
   return(INIT_SUCCEEDED);
  }
WebRequest (WINV24,M1)  Status code: 200
WebRequest (WINV24,M1)  Resposta: {
WebRequest (WINV24,M1)    "args": {}, 
WebRequest (WINV24,M1)    "data": "{\"key1\":\"value1\",\"key2\":\"value2\"}\u0000", 
WebRequest (WINV24,M1)    "files": {}, 
WebRequest (WINV24,M1)    "form": {}, 
WebRequest (WINV24,M1)    "headers": {
WebRequest (WINV24,M1)      "Accept": "*/*", 
WebRequest (WINV24,M1)      "Accept-Encoding": "gzip, deflate", 
WebRequest (WINV24,M1)      "Accept-Language": "pt,en;q=0.5", 
WebRequest (WINV24,M1)      "Content-Length": "34", 
WebRequest (WINV24,M1)      "Content-Type": "application/json", 
WebRequest (WINV24,M1)      "Host": "httpbin.org", 
WebRequest (WINV24,M1)      "User-Agent": "MetaTrader 5 Terminal/5.4476 (Windows NT 11.0.22631; x64)", 
WebRequest (WINV24,M1)      "X-Amzn-Trace-Id": "Root=1-66d9be03-59060f042f7090092787855e"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "json": null, 
WebRequest (WINV24,M1)    "origin": "200.103.20.126", 
WebRequest (WINV24,M1)    "url": "https://httpbin.org/post"
WebRequest (WINV24,M1)  }
WebRequest (WINV24,M1)  

请注意,现在发送的数据出现在“data”字段中,这表明我们已经步入正轨。但请注意,出现了“\u0000”字符,这是因为StringToCharArray方法在将字符串转换为字节数组时会包含空终止符。为了避免这种情况,我们可以调整数组的大小。让我们打印出正在发送的正文,看看这个“\u0000”字符是从哪里来的。

int OnInit()
  {
//--- Defining variables
   string method = "POST";                    // HTTP verb in string (GET, POST, etc...)
   string url = "https://httpbin.org/post";   // Destination URL
   string headers = "Content-Type: application/json";// Request header
   int timeout = 5000;                        // Maximum waiting time 5 seconds
   char data[];                               // Data we will send (body) array of type char
   char result[];                             // Data received as an array of type char
   string result_headers;                     // String with response headers

//--- Tratando body
   string body = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
   StringToCharArray(body,data,0,WHOLE_ARRAY,CP_UTF8);
   Print("Body: ",body);
   Print("Body Size: ",StringLen(body));
   ArrayPrint(data);
   Print("Array Size: ",ArraySize(data));

//--- Calling the function and getting the status_code
   int status_code = WebRequest(method,url,headers,timeout,data,result,result_headers);
   
//--- Print the data
   Print("Status code: ",status_code);
   Print("Response: ",CharArrayToString(result)); // We use CharArrayToString to display the response in string form.
   
//---
   return(INIT_SUCCEEDED);
  }
WebRequest (WINV24,M1)  Body: {"key1":"value1","key2":"value2"}
WebRequest (WINV24,M1)  Body Size: 33
WebRequest (WINV24,M1)  123  34 107 101 121  49  34  58  34 118  97 108 117 101  49  34  44  34 107 101 121  50  34  58  34 118  97 108 117 101  50  34 125   0
WebRequest (WINV24,M1)  Array Size: 34
WebRequest (WINV24,M1)  Status code: 200
WebRequest (WINV24,M1)  Resposta: {
WebRequest (WINV24,M1)    "args": {}, 
WebRequest (WINV24,M1)    "data": "{\"key1\":\"value1\",\"key2\":\"value2\"}\u0000", 
WebRequest (WINV24,M1)    "files": {}, 
WebRequest (WINV24,M1)    "form": {}, 
WebRequest (WINV24,M1)    "headers": {
WebRequest (WINV24,M1)      "Accept": "*/*", 
WebRequest (WINV24,M1)      "Accept-Encoding": "gzip, deflate", 
WebRequest (WINV24,M1)      "Accept-Language": "pt,en;q=0.5", 
WebRequest (WINV24,M1)      "Content-Length": "34", 
WebRequest (WINV24,M1)      "Content-Type": "application/json", 
WebRequest (WINV24,M1)      "Host": "httpbin.org", 
WebRequest (WINV24,M1)      "User-Agent": "MetaTrader 5 Terminal/5.4476 (Windows NT 11.0.22631; x64)", 
WebRequest (WINV24,M1)      "X-Amzn-Trace-Id": "Root=1-66d9bed3-2ebfcda024f637f436fc1d82"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "json": null, 
WebRequest (WINV24,M1)    "origin": "200.103.20.126", 
WebRequest (WINV24,M1)    "url": "https://httpbin.org/post"
WebRequest (WINV24,M1)  }
WebRequest (WINV24,M1)  

请注意,正文字符串是一个有效的JSON,也就是说,它有开闭方括号,值之间用逗号分隔,并且遵循键值对的规则。让我们看看由StringToCharArray函数生成的字节数组。请注意,它打印出了字符串和数组的大小,但它们是不同的。字节数组比字符串多一个位置。还请注意,在值的列表中,最后一个值是“0”,而它应该是125,即字符“}”。因此,为了解决这个问题,我们将使用ArrayRemove()函数移除数组的最后一个位置 。

int OnInit()
  {
//--- Defining variables
   string method = "POST";                    // HTTP verb in string (GET, POST, etc...)
   string url = "https://httpbin.org/post";   // Destination URL
   string headers = "Content-Type: application/json";// Request header
   int timeout = 5000;                        // Maximum waiting time 5 seconds
   char data[];                               // Data we will send (body) array of type char
   char result[];                             // Data received as an array of type char
   string result_headers;                     // String with response headers

//--- Tratando body
   string body = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
   StringToCharArray(body,data,0,WHOLE_ARRAY,CP_UTF8);
   ArrayRemove(data,ArraySize(data)-1);
   Print("Body: ",body);
   Print("Body Size: ",StringLen(body));
   ArrayPrint(data);
   Print("Array Size: ",ArraySize(data));

//--- Calling the function and getting the status_code
   int status_code = WebRequest(method,url,headers,timeout,data,result,result_headers);
   
//--- Print the data
   Print("Status code: ",status_code);
   Print("Response: ",CharArrayToString(result)); // We use CharArrayToString to display the response in string form.
   
//---
   return(INIT_SUCCEEDED);
  }
WebRequest (WINV24,M1)  Body: {"key1":"value1","key2":"value2"}
WebRequest (WINV24,M1)  Body Size: 33
WebRequest (WINV24,M1)  123  34 107 101 121  49  34  58  34 118  97 108 117 101  49  34  44  34 107 101 121  50  34  58  34 118  97 108 117 101  50  34 125
WebRequest (WINV24,M1)  Array Size: 33
WebRequest (WINV24,M1)  Status code: 200
WebRequest (WINV24,M1)  Resposta: {
WebRequest (WINV24,M1)    "args": {}, 
WebRequest (WINV24,M1)    "data": "{\"key1\":\"value1\",\"key2\":\"value2\"}", 
WebRequest (WINV24,M1)    "files": {}, 
WebRequest (WINV24,M1)    "form": {}, 
WebRequest (WINV24,M1)    "headers": {
WebRequest (WINV24,M1)      "Accept": "*/*", 
WebRequest (WINV24,M1)      "Accept-Encoding": "gzip, deflate", 
WebRequest (WINV24,M1)      "Accept-Language": "pt,en;q=0.5", 
WebRequest (WINV24,M1)      "Content-Length": "33", 
WebRequest (WINV24,M1)      "Content-Type": "application/json", 
WebRequest (WINV24,M1)      "Host": "httpbin.org", 
WebRequest (WINV24,M1)      "User-Agent": "MetaTrader 5 Terminal/5.4476 (Windows NT 11.0.22631; x64)", 
WebRequest (WINV24,M1)      "X-Amzn-Trace-Id": "Root=1-66d9c017-5985f48331dba63439d8192d"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "json": {
WebRequest (WINV24,M1)      "key1": "value1", 
WebRequest (WINV24,M1)      "key2": "value2"
WebRequest (WINV24,M1)    }, 
WebRequest (WINV24,M1)    "origin": "200.103.20.126", 
WebRequest (WINV24,M1)    "url": "https://httpbin.org/post"
WebRequest (WINV24,M1)  }
WebRequest (WINV24,M1)  

现在,字符串和数组的大小已经对齐,服务器也正确地将内容识别为有效的JSON。这可以在响应返回中提供,服务器将发送的JSON返回在“json”和“data”字段中。经过一些配置,我们成功地执行了一个简单的HTTP POST请求,并正确地发送了数据。然而,使用WebRequest函数并非易事;它需要对协议的工作原理以及我们正在操作的结构有深入的理解。通常,即使是很小的问题也可能变得复杂,正如我们在调整代码以获得有效响应时所看到的那样。Connexus库的目标正是简化这一过程,让用户无需处理这些底层且更抽象的编程层面,避免像我们现在面临的这种复杂问题,令WebRequest在使用上更容易且更高效。


结论

在本文中,我们详细探讨了MetaTrader 5中WebRequest函数的使用,这是开发人员扩展其EA功能与外部服务器和API进行通信的一个重要工具。我们使用httpbin.org服务作为实际示例来执行GET和POST请求,以JSON格式发送和接收数据,并理解服务器的响应。此概述只是一个通过HTTP进行系统集成的旅程的开始,为未来更复杂的项目奠定了基础。

旅程才刚刚开始。让我们一起将WebRequest函数转变为一个简化、强大且易于使用的工具,简化EA的开发,并为MetaTrader 5中的自动化和集成开辟新的可能性。

本文由MetaQuotes Ltd译自英文
原文地址: https://www.mql5.com/en/articles/15795

附加的文件 |
WebRequest.mq5 (5.14 KB)
开发回放系统(第 61 部分):玩转服务(二) 开发回放系统(第 61 部分):玩转服务(二)
在本文中,我们将研究使回放/模拟系统更高效、更安全地运行的修改。我也不会对那些想要充分利用这些类的人置之不理。此外,我们将探讨 MQL5 中的一个特定问题,即在使用类时降低代码性能,并解释如何解决它。
在MQL5中创建交易管理员面板(第二部分):增强响应性和快速消息传递 在MQL5中创建交易管理员面板(第二部分):增强响应性和快速消息传递
在本文中,我们将增强之前创建过的管理面板的响应性。此外,我们还将探讨在交易信号背景下快速消息传递的重要性。
高效处理指标的便捷方法 高效处理指标的便捷方法
在本文中,我将介绍如何制作一个简单的面板,以便直接从图表中更改指标设置,以及需要对指标进行哪些更改以连接该面板。本文面向 MQL5 的新手用户。
数据科学和机器学习(第 29 部分):为 AI 训练目的而选择最佳外汇数据的基本技巧 数据科学和机器学习(第 29 部分):为 AI 训练目的而选择最佳外汇数据的基本技巧
在本文中,我们将深入探讨选择最具相关性、及最高品质的外汇数据,从而强化 AI 模型性能的关键层面。