I want to transfer the file to the site using the POST method on the php site the script accepting "userfile" and which saves it on the server
void SendFile()
{
const char hdrs[] = "Content-Type: multipart/form-data; boundary=--------071418204214402\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
char* data1 = "----------071418204214402\n"
"Content-Disposition: form-data; name=\"userfile\"; filename=\"file.zip\"\n"
"Content-Type: text/plain\n"
"Content-Transfer-Encoding: binary\n\n";
char* filecontent = "Hello World!"; // Simulated the contents of the file with a string.
char* data2 = "\n----------071418204214402--\n";
char* allstr = calloc(strlen(data1) + strlen(filecontent) + strlen(data2) + 1, 1);
strcat(allstr, data1);
strcat(allstr, filecontent);
strcat(allstr, data2);
HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, "crazra94.beget.tech", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/send.php", NULL, NULL, NULL, 0, 1);
HttpAddRequestHeaders(hRequest, hdrs, -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
INTERNET_BUFFERS bufferIn;
LPDWORD bytesWritten;
memset(&bufferIn, 0, sizeof(INTERNET_BUFFERS));
bufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
bufferIn.dwBufferTotal = strlen(data1) + strlen(filecontent) + strlen(data2)+1;
HttpSendRequestEx(hRequest, &bufferIn, NULL, HSR_INITIATE, 0);
InternetWriteFile(hRequest, (const void*)data1, strlen(data1)+1, bytesWritten);
InternetWriteFile(hRequest, (const void*)filecontent, strlen(filecontent)+1, bytesWritten); // File
InternetWriteFile(hRequest, (const void*)data2, strlen(data2)+1, bytesWritten);
HttpEndRequest(hRequest, NULL, HSR_INITIATE, 0);
}
The file is not sent (the request is not correct) can I fix it?
UPD: This code is work correct. But i dont know how i can put the binary file to the array of chars. This is for send text files:
void SendFile()
{
const unsigned char hdrs[] = "Content-Type: multipart/form-data; boundary=--------071418204214402\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
unsigned char* data1 = "----------071418204214402\r\n"
"Content-Disposition: form-data; name=\"userfile\"; filename=\"test2.txt\"\r\n"
"Content-Type: text/plain\r\n"
"Content-Transfer-Encoding: binary\r\n\r\n";
unsigned char* filecontent = "Hello World!";
unsigned char* data2 = "\r\n----------071418204214402--\r\n";
unsigned char* allstr = calloc(strlen(data1) + strlen(filecontent) + strlen(data2) + 1, sizeof(unsigned char));
strcat(allstr, data1);
strcat(allstr, filecontent);
strcat(allstr, data2);
HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, "crazra94.beget.tech", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/send.php", NULL, NULL, NULL, 0, 1);
HttpSendRequest(hRequest, hdrs, strlen(hdrs), allstr, strlen(allstr));
}