我試圖從我正在使用HTTP POST請求到遊戲網站的遊戲發送文件。當我使用HTML頁面上的表單發送文件時,它工作正常。但是,我一直無法在遊戲中模擬相同的東西。我收到的響應代碼爲200,表示請求已成功完成,但上傳腳本指示它收到GET請求,並且$ _FILES和$ _POST數組爲空。我已經在Wireshark中查看了HTML表單的POST和遊戲POST的輸出,但一直未能檢測到兩者之間的任何有意義的差異。 (如果我知道如何完全複製HTML表單的POST方法,我會,但我認爲這是不可能的。)無法獲取HTTP POST請求以正常工作
無論如何,這裏是我用來發送它的代碼。謝謝!
HINTERNET hInternet;
HINTERNET hConnect;
HINTERNET hRequest;
INTERNET_BUFFERS Buffers;
DWORD dwBytesWritten;
DWORD dwNumBytesToWrite;
char szBoundary[32];
char szContentTypeHeader[64];
char szFileHeader[256];
char szFileFooter[4];
char szBodyFooter[32];
ULONG ulTotalBytesWritten;
ULONG ulOffset;
ULONG ulContentLength;
// Get a handle for working with the Internet.
hInternet = InternetOpen("Wrack", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet == NULL)
return (false);
// Open an HTTP session for the site.
hConnect = InternetConnect(hInternet, "wrackgame.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
if (hConnect == NULL)
{
InternetCloseHandle(hInternet);
return (false);
}
// Open the POST request.
hRequest = HttpOpenRequest(hConnect, "POST", "leaderboard/upload_file.php", NULL, "http://www.wrackgame.com/leaderboard/upload.html", NULL, 0, NULL);
if (hRequest == NULL)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
return (false);
}
// Generate our various headers and footers.
sprintf_s(szBoundary, "----%04x%04x%04x", rand() % 0xffff, rand() % 0xffff, rand() % 0xffff);
sprintf_s(szContentTypeHeader, "Content-Type: multipart/form-data; boundary=%s", szBoundary);
sprintf_s(szFileHeader, "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", szBoundary, "file", "testreplay.wrp");
sprintf_s(szFileFooter, "\r\n");
sprintf_s(szBodyFooter, "--%s--\r\n", szBoundary);
// Build our header.
if (HttpAddRequestHeaders(hRequest, szContentTypeHeader, -1, HTTP_ADDREQ_FLAG_ADD) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
// Calculate how much data we'll be sending.
ulContentLength = network_CalcContentLength(szFileHeader, szFileFooter, szBodyFooter);
// Initialize our buffers.
memset(&Buffers, 0, sizeof(INTERNET_BUFFERS));
Buffers.dwStructSize = sizeof(INTERNET_BUFFERS);
Buffers.dwBufferTotal = ulContentLength;
// Send our HTTP request.
if (HttpSendRequestEx(hRequest, &Buffers, NULL, HSR_INITIATE, NULL) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
// Send the header.
ulTotalBytesWritten = 0;
if (InternetWriteFile(hRequest, szFileHeader, strlen(szFileHeader), &dwBytesWritten) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
ulTotalBytesWritten += dwBytesWritten;
// Next, write the body of the replay.
ulOffset = 0;
while (ulOffset < (DWORD)REPLAY_GetReplaySize())
{
// Determine how many bytes of the replay to send. If we're almost
// done, send less than 1024 bytes.
dwNumBytesToWrite = min(1024, REPLAY_GetReplaySize() - ulOffset);
// Send a piece of the replay and log how many bytes we actually
// transferred.
if (InternetWriteFile(hRequest, REPLAY_GetReplayData() + ulOffset, dwNumBytesToWrite, &dwBytesWritten) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
ulTotalBytesWritten += dwBytesWritten;
// Increment the offset of the replay buffer.
ulOffset += 1024;
}
// Send our file footer.
if (InternetWriteFile(hRequest, szFileFooter, strlen(szFileFooter), &dwBytesWritten) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
ulTotalBytesWritten += dwBytesWritten;
// Send our body footer.
if (InternetWriteFile(hRequest, szBodyFooter, strlen(szBodyFooter), &dwBytesWritten) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
ulTotalBytesWritten += dwBytesWritten;
// Close our request now that we're done.
if (HttpEndRequest(hRequest, NULL, 0, NULL) == false)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return (false);
}
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
// No need to do anything more!
return (true);
我不確定它是否會有所幫助,但是您是否試過編寫一個數據塊而不是單個塊? – Deanna
@Deanna:我會怎麼做?據我所知,我必須使用幾個函數來啓動該進程,如HttpSendRequest,在使用InternetWriteFile發送數據包之前發送數據包。 – Carnevil
要開始這個過程,是的,但是你至少需要4次調用'InternetWriteFile()'這可能是導致問題的時機。 – Deanna