如何在mql4中使用wininet以正确的正文发送SendRequest

时间:2018-09-21 11:23:55

标签: wininet mql

我有一个虚拟Web服务,其URL:http://47.91.231.122:5002/token以JSON格式返回数据,我想使用mql中的WINInet APi调用此服务。致电此服务?

enter image description here

通过在呼叫正文中输入所有必需的数据来从服务器获取访问令牌。 网址:http://47.91.231.122:5002/token

用户名:admin@admin.com

密码:111111

grant_type:密码

下面是我的代码。 请指导我如何正确执行操作。谢谢

#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

标题和正文数据正确吗?

    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)
             {

根据图片,我是否正确编码以连接图片?

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

我收到一条虚拟消息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); 
    }

0 个答案:

没有答案