HttpQueryInfo ,错误 12150 没有 content length
笑客v 2017-08-11 04:49:01 extern "C" __declspec(dllexport) string GetHTML(char*URL)
{
HINTERNET Session = InternetOpenA(NULL,INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
if (NULL == Session)
{
InternetCloseHandle(Session);
return "";
}
DWORD d = GetLastError();
URL = "http://www.baidu.com";
HINTERNET hHttpFile = InternetOpenUrlA(Session, URL, NULL,NULL, INTERNET_FLAG_NO_CACHE_WRITE, NULL);
if (NULL == hHttpFile)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(Session);
CHAR szError[256];
DWORD dwErrorSize = 256;
DWORD dwLastErrorMsg;
InternetGetLastResponseInfo(&dwLastErrorMsg, (LPWSTR)szError, &dwErrorSize);
string m_strTmperror = szError;
d = GetLastError();
return "";
}
d = GetLastError();
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize = 10 * 1024;
if (::HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) == TRUE)
{
// allocating the memory space for http file contents
dwFileSize = atol(szSizeBuffer);
}
else
d = GetLastError();//总是到这里 12150 因为拿不到内容长度,数据总是不玩完整读取,不是多了就是少了,为什么会没有发现数据长度头?
PBYTE pBuf = new BYTE[dwFileSize * sizeof(TCHAR)];
if (NULL == pBuf)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(Session);
return FALSE;
}
DWORD dwReadDataLength = NULL;
BOOL bRet = TRUE;
do
{
//ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR));
bRet = InternetReadFile(hHttpFile, pBuf, dwFileSize, &dwReadDataLength);
} while (NULL != dwReadDataLength);
return (char*)pBuf;
}