How to SendRequest with correct body by using wininet in mql4

1

I have dummy web service with URL: http://47.91.231.122:5002/token which return data in JSON I want to call this service using WINInet APi in mql.Can anybody help me how to use contruct "HttpSendRequest" method to add header and data to call this service?

enter image description here

Get the access token from the server by enter all the required data in the call body. URL: http://47.91.231.122:5002/token

username:admin@admin.com

password:111111

grant_type:password

Below is my code. Please guide me how to do it correctly. Thanks

#import  "Wininet.dll"
       int InternetOpenW(string name, int config, string, string, int);
       int InternetConnectW(int hopen, string, int, string, string, int, int, int); 
       int HttpRequestW(int hconnect, string, string, int, string, int, string, int); 
       int InternetOpenUrlW(int, string, string, int, int, int);
       bool InternetReadFile(int, uchar &sBuffer[], int, int& OneInt);
       bool InternetCloseHandle(int); [enter image description here][1]
       int HttpOpenRequestA(int, string, string, string, string, string& AcceptTypes[], int, int);
       bool HttpSendRequestA(int, string, int, string, int);
    #import
    #import "kernel32.dll"
       int GetLastError(void);
    #import

is the header and body data correct?

    int init() 
    {
          string headers = "Content-Type: application/x-www-form-urlencoded\r\n";
          string data = "{\"username\":\"admin@admin.com\",\"password\":\"111111\",\"grant_type\":\"password\"}";
          string acceptTypes[1] = {"*/*"};
          int httpconnect=0;
          int HttpRequest =0;

          int httpopen = Wininet::InternetOpenW("Phoenix_API", 0, " "," ",0 ); 
          int e=kernel32::GetLastError();
          printf("HttpOpen=0 = "+httpopen+", "+e); 
          if (e==0)
             {

Base on the picture, am i code it correct to connect it?

             httpconnect = Wininet::InternetConnectW(httpopen, "47.91.231.122", 5002, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
             printf("httpconnect=0 = "+httpconnect+", "+e); 
             e=kernel32::GetLastError();
             if (e==0)
                {                                    
                   HttpRequest = HttpOpenRequestA(httpconnect, "POST", "/token", NULL, NULL, acceptTypes, 0, 1);
                   e=kernel32::GetLastError();
                   printf("HttpRequest = "+HttpRequest+", "+e);
                   if (e==0)
                      {  
                      bool A = HttpSendRequestA(HttpRequest, headers, StringLen(headers), "", 0);
                      e=kernel32::GetLastError();
                      printf("A = "+A+", "+e);  

i get a dummy message DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

                      //--- Define buffers
                      uchar ch[512]={0,0,0,0};
                      string temp="";

                      //--- Retrieve data from file
                      int cnt=0;
                      while(Wininet::InternetReadFile(HttpRequest,ch,512,cnt))
                         {
                         if(cnt<=0) break;
                         temp=temp+CharArrayToString(ch,0,cnt);
                         printf(temp);
                        } 
                      MessageBox(temp, "HTTP READ:", 0x00000030);                
                 }
            } 
       }
       //--- Close connection
       if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
       if (httpconnect > 0) InternetCloseHandle(httpconnect); 
       if (httpopen > 0) InternetCloseHandle(httpopen); 
    }
wininet
mql
asked on Stack Overflow Sep 21, 2018 by user10395466 • edited Sep 21, 2018 by user10395466

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0