I've written a custom indicator and am trying to hook it up to my authorization server. I've looked at https://docs.mql4.com/common/webrequest but of course you can't use it in an indicator, have also looked into using WinINet which just crashes, and I also tried https://www.mql5.com/en/code/10121 with no success.
Does anyone have any experience or insight into making GET calls to a REST server work inside of a custom indicator? Currently I'm at a loss, thinking it can't be done.
Only hope you have, if you can use WebRequest/dll/WinINet from OnInit().From OnCalculate() it doesn't work for sure.
Slim chance, but you can try it.
I don't know the solution, but I do know of a user that develops Indicators with an "Authorise" mechanism via a Web access, so if he does not say anything here, try to contact him (do a search for his profile name "Ovo" on the mql5.com site).
PS! I suspect however, that he uses DLL calls to the Win32 API (probably "WinINet" or "WinHTTP"), but uses asynchronous non-blocking functions so as not to pause the indicator functionality while waiting for a response.
Thansk FMIC, I sent him an email but I think I actually got it figured out! Here's the code, maybe it can help someone in the future!
//this goes at the top of the indicator
int InternetOpenW(string, int, string, string, int);
int InternetConnectW(int, string, int, string, string, int, int, int);
int InternetOpenUrlW(int, string, string, int, int, int);
int InternetReadFile(int, uchar &sBuffer, int, int& OneInt);
int HttpOpenRequestW(int, string, string, string, string, string, int, int);
bool HttpSendRequestW(int, string, int, string, int);
//this is in my OnCalculate()
//path on the server, r= TimeToStr is used for cache busting, ensuring we always get the latest value from the server (safety precaution), i.e. /Authorization?Username=mql4&uid=1234&r=...
string path = "/<endpoint>?<Query String Parameters>&r="+TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
string headers = "Content-Type: application/x-www-form-urlencoded";
string data = "";
int HttpOpen = InternetOpenW(" ", 0, " ", "", 0);
int HttpConnect = InternetConnectW(HttpOpen, "<URL without the HTTP/HTTPS, i.e. www.google.com", 80, "", "", 3, 0, 0); //80 is the port number
int HttpRequest = HttpOpenRequestW(HttpConnect, "GET", path, "", "", data, 0, 0);
bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));
string strWebPage = "";
//this should be your custom logic
if(strWebPage == "whatever you send back if authorized") //i.e. strWebPage == "LetThemIn"
authorized = true;
authorized = false;
To add comments, please log in or register