Download MetaTrader 5

upload image via HttpSendRequestW()

To add comments, please log in or register
mallymally
94
mallymally  
hi,

i try to upload an image to a http server via "post" method by using HttpSendRequestW(), but it doesnt work yet.

i get the error message in my metatrader: " Access violation read to 0x00502030 in 'Wininet.dll' "

on my http server there is a php file that saves the file.

take a look at the code

#property strict


#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, int, int);
   bool HttpSendRequestW(int, string, int, string, int);
#import


input string InpFileName="EURUSDM5.png"; //An image in folder MQL4/Files/
input string InpFileType="image/png";    //Correct mime type of the image
//+------------------------------------------------------------------+
// sending an image         |
//+------------------------------------------------------------------+
bool upload_file (string filename,string filetype)
  {
   int    res=0;     // To receive the operation execution result
   char   data[];  // Data array to send POST requests
   char   file[];  // Read the image here
   string str="";
   string auth,sep="-------Jyecslin9mp8RdKV"; // multipart data separator
//--- A file is available, try to read it
   if(filename!=NULL && filename!="")
     {
      res=FileOpen(filename,FILE_READ|FILE_BIN);
      if(res<0)
        {
         Print("Error opening the file \""+filename+"\"");
         return(false);
        }
      //--- Read file data
      if(FileReadArray(res,file)!=FileSize(res))
        {
         FileClose(res);
         Print("Error reading the file \""+filename+"\"");
         return(false);
        }
      //---
      FileClose(res);
     }
//--- Create the body of the request 
   ArrayResize(data,StringToCharArray(str,data,0,WHOLE_ARRAY,CP_UTF8)-1);
//--- Resetting error code
   ResetLastError();
//--- Authorization request


//--- If there is a data file, send it to the server
   if(ArraySize(file)!=0)
     {
      //--- Form the request body
      str="--"+sep+"\r\n";
      str+="Content-Disposition: form-data; name=\"attachedFile_imagesLoader\"; filename=\""+filename+"\"\r\n";
      str+="Content-Type: "+filetype+"\r\n\r\n";
      

      string headers = str + "Content-Type: multipart/form-data;";
      string data2 = CharArrayToString(file);
      int HttpOpen = InternetOpenW(" ", 0, " ", "", 0);  
      int HttpConnect = InternetConnectW(HttpOpen, "localhost", 80, "", "", 3, 0, 0);
      int HttpRequest = HttpOpenRequestW(HttpConnect, "POST", "dev/mt_uploadfile/upload.php", "", "", data2, 0, 0);   
      bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data2, StringLen(data2));
      int read[1];
      Print("This is the POST result: ", result);
      InternetCloseHandle(HttpOpen);
      InternetCloseHandle(HttpRequest);
     
     }
      
   return(res==200);
  }


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int init()
  {
//--- upload the image
   upload_file (InpFileName,InpFileType);

   ExpertRemove();
//----
   return(0);
  }
  
//------------------------------------
int deinit()
{
   return(0);
}

//------------------------------------
int start()
  {
//----

//----
   return(0);
  } 
//+------------------------------------------------------------------+


what have i done wrong?

To add comments, please log in or register