В приведенном выше комментарии Реми упоминаются отсутствующие заголовки. Это было исправлено вместе с несколькими другими незначительными ошибками. Вы можете сравнить это с тем, что у вас есть.
LPCTSTR szUserAgent = _T("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43");
LPCTSTR szHost = _T("discordapp.com");
LPCTSTR szUrlPath = _T("/api/webhooks/740632765195747359/m8FGH08HsJ0GzkYFKvlDFO6QE3MtzKOqIaKpZH4LmqLDJPvlLQEuQQNjr_R8x4y9zCjx");
LPCTSTR szAcceptTypes[] = { _T("application/json"), NULL };
LPCTSTR szContentTypeHeader = _T("Content-Type: application/json");
LPCSTR szPostData = "{ \"username\":\"ANDY from StackOverflow\", \"content\": \"Test string again\" }";
const DWORD dwPostDataLength = strlen(szPostData);
HINTERNET hIntSession = InternetOpen(szUserAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hIntSession) {
HINTERNET hHttpSession = InternetConnect(hIntSession, szHost,
INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, NULL);
if (hHttpSession) {
HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, _T("POST"), szUrlPath,
NULL, NULL, szAcceptTypes,
(INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE |
INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE), 0);
if (hHttpRequest) {
if (HttpSendRequest(hHttpRequest,
szContentTypeHeader, -1, (LPVOID)szPostData,
dwPostDataLength)) {
DWORD dwStatusCode = 0;
{
TCHAR szStatusCode[32] = { 0 };
DWORD dwStatusCodeSize = sizeof(szStatusCode) / sizeof(TCHAR);
if (HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE,
szStatusCode, &dwStatusCodeSize, NULL)) {
dwStatusCode = _ttoi(szStatusCode);
}
}
//
// dwStatusCode: [200,299] means success, any other value
// means something went wrong
//
unsigned char* lpResponse = NULL;
DWORD dwTotalSize = 0;
{
unsigned char* lpBuffer = (unsigned char*)malloc(2048);
DWORD dwRead = 0;
while (InternetReadFile(hHttpRequest, lpBuffer, sizeof(lpBuffer), &dwRead) && dwRead) {
unsigned char* lpTmp = (unsigned char*)realloc(lpResponse, dwTotalSize + dwRead);
if (lpTmp) {
lpResponse = lpTmp;
memcpy(&lpResponse[dwTotalSize], lpBuffer, dwRead);
dwTotalSize += dwRead;
}
dwRead = 0;
}
free(lpBuffer);
}
//
// lpResponse now has your data with length of dwTotalSize.
// Do with it what you want.
// Keep in mind that a successful request will return
// no data.
//
if (lpResponse) { free(lpResponse); }
}
InternetCloseHandle(hHttpRequest);
}
InternetCloseHandle(hHttpSession);
}
InternetCloseHandle(hIntSession);
}