Sending Webrequest Metatrader WinAPI

 

Hello, i dont find with the serach funktion example how to import WinAPI functions into metatrader for makeing get and post webrequest, is there a example here anywhere?

I want to slove this problem which you have with EAs when you use the standard Webrequest function from MQL, which is blocking the EA thread i think when i use the WinAPI DLL it should not block the EA Thread anymore or?

Also something like sending parallel more then one Webrequest should work with that i think.

 
Metatrader Account:

Hello, i dont find with the serach funktion example how to import WinAPI functions into metatrader for makeing get and post webrequest, is there a example here anywhere?

I want to slove this problem which you have with EAs when you use the standard Webrequest function from MQL, which is blocking the EA thread i think when i use the WinAPI DLL it should not block the EA Thread anymore or?

Also something like sending parallel more then one Webrequest should work with that i think.

hi, for using webrequest you should allow the URL in the metatrader settings  experts tab.  and allow connection to   https://domain.com

then you can have 2 types of connections , GET and POST

also you can send 2 types of queries ,  1 )  query parameters in the URL  ,   2) sending parameters to server with headers or body  fields using application\json


you can find sample  code here : 

https://docs.mql4.com/common/webrequest

WebRequest - Common Functions - MQL4 Reference
WebRequest - Common Functions - MQL4 Reference
  • docs.mql4.com
WebRequest - Common Functions - MQL4 Reference
 
Farzin Sadeghi Bonjar #:

hi, for using webrequest you should allow the URL in the metatrader settings  experts tab.  and allow connection to   https://domain.com

then you can have 2 types of connections , GET and POST

also you can send 2 types of queries ,  1 )  query parameters in the URL  ,   2) sending parameters to server with headers or body  fields using application\json


you can find sample  code here : 

https://docs.mql4.com/common/webrequest

thank you amigo, but it looks like you have completely notread and understand my question, i want to use WinApi function because i say the MQl Webrequest function is blocking EAs

 

because you don't describe your problem clearly , or atleast send some sample codes.

MQL webrequest  is generated and sent  by EAs. and I have wrote some codes with webrequest , and api even wordpress api and I don't have blocking problems. 

if you explain more, or show a part of your code I can be more helpful.

 
Farzin Sadeghi Bonjar #:

because you don't describe your problem clearly , or atleast send some sample codes.

MQL webrequest  is generated and sent  by EAs. and I have wrote some codes with webrequest , and api even wordpress api and I don't have blocking problems. 

if you explain more, or show a part of your code I can be more helpful.

the thing is you dont even know what blocking EA means thats why you dont have understand my question at all

 

Hey guys, i have here a example code for using the WinAPI function to send get webrequest, can somebody show me how i could create a post webrequest with this?

//+------------------------------------------------------------------+
//| Imported functions description                                   |
//+------------------------------------------------------------------+
#import "wininet.dll"
int InternetAttemptConnect(int x);
int InternetOpenW(string sAgent,int lAccessType,
                  string sProxyName="",string sProxyBypass="",
                  int lFlags=0);
int InternetOpenUrlW(int hInternetSession,string sUrl,
                     string sHeaders="",int lHeadersLength=0,
                     int lFlags=0,int lContext=0);

int InternetReadFile(int hFile, uchar &sBuffer[],int lNumBytesToRead,int &lNumberOfBytesRead[]);
//int InternetReadFile(int hFile, string sBuffer, int lNumBytesToRead, int &lNumberOfBytesRead[]);
int InternetReadFile(
                     int     hFile,
                     uchar     &sBuffer[],
                     int     lNumBytesToRead,
                     int     &lNumberOfBytesRead
                     );
int HttpQueryInfoW(int hRequest,int dwInfoLevel,
                   uchar &lpvBuffer[],int &lpdwBufferLength,int &lpdwIndex);
int InternetCloseHandle(int hInet);
#import
#define HTTP_QUERY_CONTENT_LENGTH 5
#define INTERNET_FLAG_RELOAD            0x80000000
int hSession_IEType;
int hSession_Direct;
int Internet_Open_Type_Preconfig=0;
int Internet_Open_Type_Direct=1;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int hSession(bool Direct)
{
//---
  string InternetAgent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
  if(Direct)
    {
      if(hSession_Direct==0)
        {
         hSession_Direct=InternetOpenW(InternetAgent,Internet_Open_Type_Direct,"0","0",0);
        }

    return(hSession_Direct);
    }
  else
   {
      if(hSession_IEType==0)
        {
         hSession_IEType=InternetOpenW(InternetAgent,Internet_Open_Type_Preconfig,"0","0",0);
        }

    return(hSession_IEType);
   }
//---
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string httpGET(string strUrl)
{
//---
   int handler=hSession(false);
   int response= InternetOpenUrlW(handler,strUrl);
   if(response == 0)return("");

   uchar ch[100]; string toStr=""; int dwBytes,h=-1;
   while(InternetReadFile(response,ch,100,dwBytes))
     {
      if(dwBytes<=0) break; toStr=toStr+CharArrayToString(ch,0,dwBytes);
     }

   InternetCloseHandle(response);
   return(toStr);
//---
}
 
Metatrader Account #:

the thing is you dont even know what blocking EA means thats why you dont have understand my question at all

WebRequest

about blocking the thread here is what documentation of webrequest says : 


The WebRequest() function is synchronous, which means its breaks the program execution and waits for the response from the requested server. Since the delays in receiving a response can be large, the function is not available for calls from the indicators, because indicators run in a common thread shared by all indicators and charts on one symbol. Indicator performance delay on one of the charts of a symbol may stop updating of all charts of the same symbol.

The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If you try to call the function from an indicator, GetLastError() will return error 4060 – "Function is not allowed for call".

WebRequest() cannot be executed in the Strategy Tester.

 
Farzin Sadeghi Bonjar #:
WebRequest

about blocking the thread here is what documentation of webrequest says : 


The WebRequest() function is synchronous, which means its breaks the program execution and waits for the response from the requested server. Since the delays in receiving a response can be large, the function is not available for calls from the indicators, because indicators run in a common thread shared by all indicators and charts on one symbol. Indicator performance delay on one of the charts of a symbol may stop updating of all charts of the same symbol.

The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If you try to call the function from an indicator, GetLastError() will return error 4060 – "Function is not allowed for call".

WebRequest() cannot be executed in the Strategy Tester.

I know that all abouth MQl Webrequest function.

If you want try to help, i am looking for doing a Post webrequest with the WinAPI functions, you can see in my last post how a Get webrequest can be done with WinApi functions, so if you know how a post Webrequest can be done with them, that is what i look for currently

 

I have found a solution now how to send post webrequest from metatrader with wininet function:

#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#import  "Wininet.dll"
   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& response[], int, int& OneInt);
   int InternetCloseHandle(int); 
   int HttpOpenRequestW(int, string, string, string, string, string& AcceptTypes[], int, int);
  bool HttpSendRequestW(int, string, int, uchar& arr[], int);
#import
#import "kernel32.dll"
int GetLastError(void);
#import


void OnStart()
{
   string acceptTypes[1] = {"*/*"}; 
   string headers = "Content-Type: application/json\r\n";
   //string headers = "Content-Type: application/x-www-form-urlencoded\r\n";
   string JSON_string2 = "{\"username\":\"admin@admin.com\"}";
   uchar charArray[];

   StringToCharArray( JSON_string2,   charArray, 0, StringLen(  JSON_string2   ) );
   int HttpOpen = InternetOpenW("HTTP_Client_Sample", 1, "", "", 0);  
   int HttpConnect = InternetConnectW(HttpOpen, "12.34.56.129", 3000, "", "", 3, 0, 1);
   int HttpRequest = HttpOpenRequestW(HttpConnect, "POST", "/testrouter", "HTTP/1.1", "", acceptTypes, 0, 1);
   bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), charArray, ArraySize(charArray));
   if(result) {
       int bytesRead;
       string serverResponse;
       int bufferSize = 1024; // Change the buffer size according to your needs
       uchar responseBuffer[];
       ArrayResize(responseBuffer, bufferSize);

       // Read the response from the server
       bool readSuccess = InternetReadFile(HttpRequest, responseBuffer, bufferSize, bytesRead);
       if(readSuccess) {
           serverResponse = CharArrayToString(responseBuffer, 0, bytesRead);
           Alert("Server Response: ", serverResponse);
       } else {
           Alert("Failed to read server response.");
       }
   }
   Alert("Last MSDN Error =: ", GetLastError());
   Alert("This is the POST result: ", result);
   if (HttpOpen > 0)InternetCloseHandle(HttpOpen);
   if (HttpRequest > 0)InternetCloseHandle(HttpRequest);
}
Reason: