#import "wininet.dll"
bool HttpPOST(string host, string script, string params[][], string filenames[][], string& strWebPage)
{
int hIntrn = InternetOpenA(AGENT, INTERNET_OPEN_TYPE_DIRECT, "0", "0", 0);
if (hIntrn == 0) {
return (false);
}
int hConn = InternetConnectA(hIntrn,
host,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
NULL);
if (hConn == 0) {
return (false);
}
int dwOpenRequestFlags = // _IGNORE_REDIRECT_TO_HTTP |
// _IGNORE_REDIRECT_TO_HTTPS |
// INTERNET_FLAG_KEEP_CONNECTION |
// INTERNET_FLAG_NO_AUTO_REDIRECT |
// INTERNET_FLAG_NO_COOKIES |
// INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_RELOAD;
string accept[] = {"Accept: text/*\r\n"};
int hReq = HttpOpenRequestA(hConn,
"POST",
script,
"HTTP/1.0",
NULL,
accept,
dwOpenRequestFlags,
NULL);
string strBoundary = "---------------------------HOFSTADTER";
string strContentHeader = "Content-Type: multipart/form-data; boundary=" + strBoundary;
HttpAddRequestHeadersA(hReq, strContentHeader, StringLen(strContentHeader), HTTP_ADDREQ_FLAG_REPLACE);
int i = 0,
idx = 0,
r = 0,
len = 0
;
string hdr = "";
int _req = FileOpen(REQUEST_FILE, FILE_BIN|FILE_WRITE);
if(_req <= 0) {
return (false);
}
// Add parameters to request body
for (i = ArrayRange(params, 0) - 1; i >= 0; i--) {
hdr = StringConcatenate(
"--", strBoundary, "\r\n",
"Content-Disposition: form-data; name=\"", params[i][0], "\"\r\n\r\n",
params[i][1], "\r\n");
FileWriteString(_req, hdr, StringLen(hdr));
}
// Add files to request body
for (i = ArrayRange(filenames, 0) - 1; i >= 0; i--) {
hdr = StringConcatenate(
"--", strBoundary, "\r\n",
"Content-Disposition: form-data; name=\"", filenames[i][0], "\"; filename=\"", filenames[i][1], "\"\r\n",
"Content-Type: application/octet-stream\r\n\r\n");
FileWriteString(_req, hdr, StringLen(hdr));
int handle = FileOpen(filenames[i][1], FILE_BIN|FILE_READ);
if(handle <= 0) {
return (false);
}
len = FileSize(handle);
for (int b = 0; b < len; b++) {
FileWriteInteger(_req, FileReadInteger(handle, CHAR_VALUE), CHAR_VALUE);
}
FileClose(handle);
}
string boundaryEnd = "\r\n--" + strBoundary + "--\r\n";
FileWriteString(_req, boundaryEnd, StringLen(boundaryEnd));
FileClose(_req);
// Re-reads saved POST data byte-to-byte from file in the pseudo-character array
// we need to send with HttpSendRequestA. This is due to the fact I know no clean
// way to cast strings _plus_ binary file contents to a character array in MQL.
// If you know how to do it properly feel free to contact me.
int request[];
_req = FileOpen(REQUEST_FILE, FILE_BIN|FILE_READ);
if (_req <= 0) {
return (false);
}
len = FileSize(_req);
ArrayResize(request, len);
ArrayInitialize(request, 0);
for (i = 0; i < len; i++) {
request[r] |= FileReadInteger(_req, CHAR_VALUE) << (idx * 8);
idx = (idx + 1) %4;
if (idx == 0) {
r++;
}
}
FileClose(_req);
if (!HttpSendRequestA(hReq, NULL, 0, request, len)) {
return (false);
}
// Read response
int lReturn[] = {1};
string sBuffer = "";
while (TRUE) {
if (InternetReadFile(hReq, sBuffer, BUFSIZ, lReturn) <= 0 || lReturn[0] == 0) {
break;
}
strWebPage = StringConcatenate(strWebPage, StringSubstr(sBuffer, 0, lReturn[0]));
}
InternetCloseHandle(hReq);
InternetCloseHandle(hConn);
InternetCloseHandle(hIntrn);
return (true);
}