WiNETC++的问题
有人能找到这个函数中的问题吗? 我的应用程序发出多个请求,如果第一个请求使用SSL,应用程序会在某些计算机上崩溃。在我的4台计算机+vmware上,它工作正常,不会崩溃 这是代码WiNETC++的问题,c++,wininet,C++,Wininet,有人能找到这个函数中的问题吗? 我的应用程序发出多个请求,如果第一个请求使用SSL,应用程序会在某些计算机上崩溃。在我的4台计算机+vmware上,它工作正常,不会崩溃 这是代码 char Buffer[1024]; DWORD dwRead; string data; string Request(string method, string host, string file, string headers, string post, bool debug, b
char Buffer[1024];
DWORD dwRead;
string data;
string Request(string method, string host, string file, string headers,
string post, bool debug, bool SSL)
{
HINTERNET hSession, hDownload, hRequest;
DWORD flag;
DWORD port;
data.empty();
//SSL or not + flag :)
if (SSL)
{
port = INTERNET_DEFAULT_HTTPS_PORT;
flag = INTERNET_FLAG_SECURE; // FLAG_SECURE
}
else
{
port = INTERNET_DEFAULT_HTTP_PORT;
flag = INTERNET_FLAG_RELOAD; //FLAG_RELOAD
}
char * postdata;
postdata = new char[post.size() + 1];
strcpy(postdata, post.c_str());
char * headersdata;
headersdata = new char[headers.size() + 1];
strcpy(headersdata, headers.c_str());
//Actual request
hSession
= InternetOpen(
"Mozilla/5.0 (Windows; U; Windows NT 6.1; sl; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11",
INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hSession)
{
hDownload = InternetConnect(hSession, host.c_str(), port, NULL, NULL,
INTERNET_SERVICE_HTTP, 0, 0);
if (hDownload)
{
hRequest = HttpOpenRequest(hDownload, method.c_str(), file.c_str(),
"HTTP/1.1", NULL, NULL, flag, 0);
if (hRequest)
{
if (strlen(headersdata) && strlen(postdata))
{
HttpSendRequest(hRequest, headersdata, strlen(headersdata),
postdata, strlen(postdata));
}
else
{
HttpSendRequest(hRequest, NULL, 0, NULL, 0);
}
}
}
}
//Writing HTML response in data buffer
while (InternetReadFile(hRequest, Buffer, sizeof(Buffer), &dwRead))
{
if (dwRead == 0)
{
break;
}
Buffer[dwRead] = 0;
data += Buffer;
}
//Debug :)
if (debug)
{
ofstream dbgfile;
dbgfile.open("debug.html");
dbgfile << data;
dbgfile.close();
}
//Close handles
InternetCloseHandle(hSession);
InternetCloseHandle(hDownload);
InternetCloseHandle(hRequest);
return data;
}
谢谢。首先,缓冲区溢出 考虑以下几行:
while (InternetReadFile(hRequest, Buffer, sizeof(Buffer), &dwRead))
及
由于在前一行中传递sizeofBuffer作为dwNumberOfBytesToRead参数,因此dwRead的最大值为sizeofBuffer。如果发生这种情况,后一行将在缓冲区结束后写入一个字节。您的数据布局不太可能导致崩溃,但这纯属偶然!,除非启用了运行时安全检查,这可以解释崩溃消息
此外,据我所知,此应用程序已请求运行时以一种不寻常的方式终止它,该消息在Microsoft实现中通过assert或terminate显示。我目前没有MSVC,无法验证。我看不出这段代码中出现这两种情况的原因,因此如果不是缓冲区溢出,也可以在其他地方查找。尝试删除strlen:
HttpSendRequest(hRequest, &headers.front(), headers.size(),
&post.front(), post.size());
在这种情况下,该功能将更加安全
无论如何,考虑使用。在这种情况下,您可以从“某些计算机”获取的崩溃转储中检查调用堆栈。崩溃发生时会出现什么错误?访问违规?其他错误代码?错误是此应用程序已请求运行时以异常方式终止它。太糟糕了,它没有在这里发生,所以我可以调试:你可以尝试远程调试…谢谢你告诉我我修复了它:但它仍然不能解决问题…:我不知道为什么它只在一些机器上崩溃,而不是在所有机器上。。。
HttpSendRequest(hRequest, &headers.front(), headers.size(),
&post.front(), post.size());