how to send request with correct body by using wininet in mql4?

 

Hi,


i wish to get data from web api.


1.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


And here is my code, not sure where is the bug, i suspect my body data is not correct.

i got the dummy message like <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">


#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); 

   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


#define INTERNET_SERVICE_HTTP           3

#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100 // Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy.

#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000 // Does not add the returned entity to the cache. 

#define INTERNET_FLAG_RELOAD            0x80000000 // Forces a download of the requested file, object, or directory listing from the origin server, not from the cache.


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 HttpRequest =0;

      

      int httpopen = Wininet::InternetOpenW("Phoenix_API", 0, " "," ",0 ); 

      int e=kernel32::GetLastError();

      printf("HttpOpen=0 = "+httpopen+", "+e); 

      if (e==0)

         {

         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 = Wininet::InternetOpenUrlW(httpopen,"http://47.91.231.122:5002/token", headers, StringLen(headers), INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_RELOAD, 0);

//            e=kernel32::GetLastError();

//            printf("httprequest=0 = "+httprequest+", "+e); 

//            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);  

     

                  //--- 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);                

                  }

//               }

            }

         } 

}


Need some1 for help. thanks.