Scripts: HTTP using WinINet directly in mql4

 

HTTP using WinINet directly in mql4:

This is a "conceptual" script that uses the WinINet functions to load a HTTP page directly from mql4, without a C++ dll

Author: Marco

 
Hi mapick!
Thans for the code. I want to ask you, can WinInet send POST data, like Ajax Http POST method? (Not file but some Json data.) If you write test code for this, please share with us. Thank you!
 

hi and thanks to you for your interest!

yes, you can send any type of request, you can try in the script, using the verb POST instead of the GET, and changing the http://... url, adding your parameters, i.e.: "http://mysite.com?par1=val1&par2=val2"

if you post here the example of what you need, I can adapt the script

 

Mapick, thanks for your help! But I think, do not change this nice simple code, if you write new, add it in another file.

Here are the details. I have java code that waits for be called by Amibroker via Ajax Json requests, and i want to reuse it with mt4. But the data must be sent by POST (in the header part of the http request), and i cannot write "http://mysite.com?par1=val1&par2=val2", because it would be too long, and also the java code would have to rewrite. Here is the javascript from Amibroker that calls java (ObjectToSend is a javascript object, that has ohlc data, class name, function name, etc., and it is sent after converting to a looong Json string. Target is simply "javaremote.json"). So i want to POST from mq4 to java a Json string (and waiting for the response), without c++ and dll coding.


<%
    function getObjectReceivedByAjax(ObjectToSend, Target) {
        request = new ActiveXObject("Msxml2.XMLHTTP");
        request.open("POST", "http://127.0.0.1:7777/" + Target, false);
        request.send(JSON.stringify(ObjectToSend));
        ObjectReceived = JSON.parse(request.responseText);
        return ObjectReceived;
    }
%>
 

yes! there is the function to add the header you want, I adjust the code and I post it here, they are only a couple of lines to add to the script

 
Very interesting, Can petitions be sent by post? What happens if the client does not have ....?
Wininet.dll
 
flaab:
Very interesting, Can petitions be sent by post? What happens if the client does not have ....?
Wininet.dll


hi and thanks! the WinINet.dll should be already included from XP onward, however if you look for "download wininet.dll" you or the EA user can easily find it, for example from http://www.dll-files.com/dllindex/dll-files.shtml?wininet

 
erzo:

Mapick, thanks for your help! But I think, do not change this nice simple code, if you write new, add it in another file.

Here are the details. I have java code that waits for be called by Amibroker via Ajax Json requests, and i want to reuse it with mt4. But the data must be sent by POST (in the header part of the http request), and i cannot write "http://mysite.com?par1=val1&par2=val2", because it would be too long, and also the java code would have to rewrite. Here is the javascript from Amibroker that calls java (ObjectToSend is a javascript object, that has ohlc data, class name, function name, etc., and it is sent after converting to a looong Json string. Target is simply "javaremote.json"). So i want to POST from mq4 to java a Json string (and waiting for the response), without c++ and dll coding.


<%
    function getObjectReceivedByAjax(ObjectToSend, Target) {
        request = new ActiveXObject("Msxml2.XMLHTTP");
        request.open("POST", "http://127.0.0.1:7777/" + Target, false);
        request.send(JSON.stringify(ObjectToSend));
        ObjectReceived = JSON.parse(request.responseText);
        return ObjectReceived;
    }
%>


to send your HEADER you should modify the code in this way:

1) send your USERNAME and PASSWORD if any to login page, or to home page

   int HttpConnect = InternetConnectA(HttpOpen, "http://javaremote.json/YOUR_LOGIN_PAGE", 80, "USERNAME", "PASSWORD", 3, 0, 1); 

otherwise you can let the empty strings as in the actual code

2) send your header, in case change the URL if the page is different from login or home page

   string header = StringConcatenate("<%",
                                       "function getObjectReceivedByAjax(ObjectToSend, Target) {",
                                       "request = new ActiveXObject(\"Msxml2.XMLHTTP\");",
                                       "request.open(\"POST\", \"http://127.0.0.1:7777/\" + Target, false);",
                                       "request.send(JSON.stringify(ObjectToSend));",
                                       "ObjectReceived = JSON.parse(request.responseText);",
                                       "return ObjectReceived;",
                                       "}",
                                      "%>");
   int HttpRequest = InternetOpenUrlA(HttpOpen, "http://javaremote.json", header, StringLen(header), 0, 0);

3) ...tell me if it works... otherwise I'll check and adjust the code!

 
Mapick, i tred your solution, but it did not work. I searched a little, and found this: http://support.microsoft.com/kb/165298

It solved everything i needed. Here is my code modification that works fine:

#import  "Wininet.dll"
   ...
   int HttpOpenRequestA(int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestA(int, string, int, string, int);
   ...
#import
int init()
{
   //----
   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "{\"ohlcVectors\":{\"ticker\":\"dax\",\"barIntervalSeconds\":300,\"open\":[6844.2998046875," +
      "6847.7998046875]},\"className\":\"MAIndicator\",\"parameters\":{\"periods\":2}}";
   string acceptTypes[1] = {"*/*"};
   int HttpOpen = InternetOpenA("HTTP_Client_Sample", 1, NULL, NULL, 0); 
   int HttpConnect = InternetConnectA(HttpOpen, "127.0.0.1", 7777, NULL, NULL, 3, 0, 1);
   int HttpRequest = HttpOpenRequestA(HttpConnect, "POST", "/javaremote.json", NULL, NULL, acceptTypes, 0, 1);
   HttpSendRequestA(HttpRequest, headers, StringLen(headers), data, StringLen(data));
   
   int read[1];
   ...
 
erzo:
Mapick, i tred your solution, but it did not work. I searched a little, and found this: http://support.microsoft.com/kb/165298

It solved everything i needed. Here is my code modification that works fine

......

Thanks Erzo! Your is a very useful contribution!

 

Nice contribution Mapick, could you give an example of how to send parameters using POST and retrieve the result?

Cheers and thanks.

Reason: