我正试图从我的c ++项目中将一些数据发送到php脚本,如下所示:
void sendThatBitch()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
vector <string> vFileContent;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
{
cout << "WinHttpOpen\n";
hConnect = WinHttpConnect(hSession, L"example.com", INTERNET_DEFAULT_HTTP_PORT, 0);
}
// Create an HTTP request handle.
if (hConnect)
{
cout << "WinHttpConnect\n";
string req = "/script.php?data1=";
req += DATA1;
req += "&data2=";
req += DATA2;
req += '\0';
WCHAR* str = new WCHAR(req.size() - 1);
MultiByteToWideChar(0, 0, req.c_str(), req.size(), str, req.size());
hRequest = WinHttpOpenRequest(hConnect, L"GET", str, NULL, WINHTTP_NO_REFERER, NULL, NULL);
}
// Send a request.
if (hRequest)
{
cout << "WinHttpOpenRequest\n";
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
}
// End the request.
if (bResults)
{
cout << "WinHttpSendRequest\n";
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
// Close any open handles.
if (hRequest)
{
WinHttpCloseHandle(hRequest);
}
if (hConnect)
{
WinHttpCloseHandle(hConnect);
}
if (hSession)
{
WinHttpCloseHandle(hSession);
}
}
现在我的问题出现在大部分时间,但并非总是如此。有时数据会被发送,但是当我收到错误时,它看起来像这样:
检测到严重错误c0000374 application.exe已触发a 断点。
application.exe中0x77EFE653(ntdll.dll)的第一次机会异常: 0xC0000374:堆已损坏(参数:0x77F34268)。
application.exe中0x77EFE653(ntdll.dll)的未处理异常: 0xC0000374:堆已损坏(参数:0x77F34268)。
在WinHttpOpenRequest
或WinHttpSendRequest
上发生这种情况。有没有人知道为什么大部分时间都会发生这种情况?
注意:此代码中提到的域名是伪造的,而不是真实代码中使用的域名。
答案 0 :(得分:0)
我在上面的评论中关注了ChrisWard1000的链接,现在正在这样做:
if (hConnect)
{
cout << "WinHttpConnect\n";
string req = "/script.php?data1=";
req += DATA1;
req += "&data2=";
req += DATA2;
req += '\0';
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, req.c_str(), -1, NULL, 0);
wchar_t* str = new wchar_t[wchars_num];
MultiByteToWideChar(0, 0, req.c_str(), req.size(), str, req.size());
hRequest = WinHttpOpenRequest( hConnect, L"GET", str, NULL, WINHTTP_NO_REFERER, NULL, NULL);
delete[] str;
}
+1的建议,谢谢你们。