MQL5Book/Scripts/p7/WebRequestTest.mq5

54 lines
2 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:09:41 +02:00
//+------------------------------------------------------------------+
//| WebRequestTest.mq5 |
//| Copyright 2022, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property description "Request a web-page."
#property description "NB: Default 'Address' requires to allow 'httpbin.org' in terminal settings - to use other addresses, change settings accordingly."
#property script_show_inputs
#include "..\..\Include\PRTF.mqh"
#include "..\..\Include\StringUtils.mqh"
#include "..\..\Include\URL.mqh"
#include "..\..\Include\HTTPHeader.mqh"
input string Method = "GET"; // Method (GET,POST,HEAD)
input string Address = "https://httpbin.org/headers";
input string Headers;
input int Timeout = 5000;
input bool DumpDataToFiles = true;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
uchar data[], result[];
string response;
int code = PRTF(WebRequest(Method, Address, Headers, Timeout, data, result, response));
Print(response);
if(code > -1)
{
if(ArraySize(result) > 0)
{
PrintFormat("Got data: %d bytes", ArraySize(result));
if(DumpDataToFiles)
{
string parts[];
URL::parse(Address, parts);
const string filename = parts[URL_HOST] +
(StringLen(parts[URL_PATH]) > 1 ? parts[URL_PATH] : "/_index_.htm");
Print("Saving ", filename);
PRTF(FileSave(filename, result));
}
else
{
Print(CharArrayToString(result, 0, 80, CP_UTF8));
}
}
}
}
//+------------------------------------------------------------------+