Simple POST/GET HttpRequest with mql4

 

Hello everyone,

I know this topic was discussed many times. But all articles I found on that topic were unfortunately too complicated or produce error message because they are too old. Therefore I wasn't able to create a script that does a simple POST/GET request.

So, I simply want to send POST and GET request to a server. The target file is a simple php file.

This is my first testscript I produced which is based on https://www.mql5.com/en/code:

#import  "Wininet.dll"
   int InternetOpenA(string, int, string, string, int);
   int InternetConnectA(int, string, int, string, string, int, int, int); 
   int InternetOpenUrlA(int, string, string, int, int, int);
   int InternetReadFile(int, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
   int HttpOpenRequestA(int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestA(int, string, int, string, int);
#import
 

int OnInit()
{
   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenA("HTTP_Client_Sample", 1, NULL, NULL, 0); 
   int HttpConnect = InternetConnectA(HttpOpen, "http://localhost/tradex", 7777, NULL, NULL, 3, 0, 1);
   int HttpRequest = HttpOpenRequestA(HttpConnect, "POST", "/index.php", "HTTP/1.1", NULL, acceptTypes, 0, 1);
   string result = HttpSendRequestA(HttpRequest, headers, StringLen(headers), data, StringLen(data));

   int read[1];
   Print("This is the POST result: " + result);

   return(0);
}

Unfortunately I receive as a result "0" which means the request was not successful. But I can't find out why.

Hope you can help me.

Thanks,

Alex

 

from the nature of the problem you are describing i'm guessing you use B600 >

so you have to adapt your code using UNICODE for example

int InternetOpenW(...

and not (ANSI)

int InternetOpenA(...
 
coolex:

Hello everyone,

I know this topic was discussed many times. But all articles I found on that topic were unfortunately too complicated or produce error message because they are too old. Therefore I wasn't able to create a script that does a simple POST/GET request.

So, I simply want to send POST and GET request to a server. The target file is a simple php file.

This is my first testscript I produced which is based on https://www.mql5.com/en/code:

Unfortunately I receive as a result "0" which means the request was not successful. But I can't find out why.

Hope you can help me.

Thanks,

Alex


https://www.mql5.com/en/forum/149321
 

OK. Thanks for the fast reply. Haven't tested it yet but could you please give some more details? Sorry for asking, but what is B600? Furthermore I can't find documentation for the "InternetOpenW" or the "InternetOpenA" functions on http://msdn.microsoft.com/en-us/library/windows/desktop/aa385098(v=vs.85).aspx

Where does the "...A" and "...W" come from?

Thanks for helping.

 
coolex:

but what is B600?

version 600 and above (MetaTrader) = build 600 and above = B600 >

coolex:

Where does the "...A" and "...W" come from?

i hope you can read

in the very same page in the link you provided says (at the bottom):

Unicode and ANSI names InternetOpenUrlW (Unicode) and InternetOpenUrlA (ANSI)

 
OK, I couldn't find the functions "InternetOpenW" or "InternetOpenA" on the site http://msdn.microsoft.com/en-us/library/windows/desktop/aa385098(v=vs.85).aspx because they do not exist there but "InternetOpenUrlW" and "InternetOpenUrlA" exist of course.
That's why I was asking for "...W" and "...A" and reading is not the problem :-).

The core function is "HttpOpenRequest" because with it's help I can send POST and GET request to an URL. Here is the doc: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384233(v=vs.85).aspx
But I don't know whether I should use "HttpOpenRequestA" or "HttpOpenRequestW" because when I use "HttpOpenRequestW" I receive the error message: "Access violation read to 0x0000007B in 'Wininet.dll'". Using "HttpOpenRequestA" I still have the same problem as at the beginning.

So, actually I think that for POST/GET request I don't need the function "InternetOpenUrlW" at all.

This is the current code which is still not working :-( :

#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  InternetCloseHandle(int); 
   int  HttpOpenRequestA (int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestW(int, string, int, string, int);
#import
 

int OnInit()
{
   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenW("HTTP_Client_Sample", 1, NULL, NULL, 0);  
   int HttpConnect = InternetConnectW(HttpOpen, "http://localhost/tradex", 7777, NULL, NULL, 3, 0, 1);
   int HttpRequest = HttpOpenRequestA(HttpConnect, "POST", "/index.php", "HTTP/1.1", NULL, acceptTypes, 0, 1);
   string result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));

   int read[1];
   Print("This is the POST result: " + result);

   return(0);
}
 

i wrote above that you need to adapt your code for UNICODE and i gave you just one example and you still left things you need to adapt for

 int  HttpOpenRequestA (int, string, string, string, string, string& AcceptTypes[], int, int);

and i'm telling you in advance that it's not all, there is more

 

Hi qjol,

in my previous post I wrote that changing "HttpOpenRequestA" into "HttpOpenRequestW" causes the error "Access violation read to 0x0000007B in 'Wininet.dll'".

All other functions I already changed to UNICODE.

I am sorry if I misunderstood but what else is missing or is wrong for a simple POST/GET request?

 
take a look here
 

Hi qjol,

thanks for the link which is very helpful. I can see that to GET data from an URL seems to work but I still don't know how to POST data. Your script defines the function "HttpOpenRequestW" but does not use it. Furthermore the script only retreives data which is working great.

But still how can I POST data? It should work with "HttpOpenRequestW" but as I already posted before I get the error message: "Access violation read to 0x0000007B in 'Wininet.dll'" and "HttpOpenRequestA" obviously is not working.

Hope you can help.


 

i decided to check your code and i fixed it but since result = false, i checked it out and i'm getting error code 12005 and according to microsoft's error code list it means

ERROR_INTERNET_INVALID_URL

12005

The URL is invalid.


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

   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenW("HTTP_Client_Sample", 1, "", "", 0);  
   int HttpConnect = InternetConnectW(HttpOpen, "http://localhost/tradex", 7777, "", "", 3, 0, 1);
   int HttpRequest = HttpOpenRequestW(HttpConnect, "POST", "/index.php", "HTTP/1.1", "", acceptTypes, 0, 1);
   bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));
   Alert ("Last MSDN Error =: ", kernel32::GetLastError());
   
   int read[1]; // not used
   Print("This is the POST result: ", result);
   if (HttpOpen > 0)
   InternetCloseHandle(HttpOpen);
   if (HttpRequest > 0)
   InternetCloseHandle(HttpRequest);
   
   return;