调用:InternetReadFile(hHttpFile,szTemp+dwSizeOfRq,dwByteToRead-dwSizeOfRq,&dwAck)
函数原型:
BOOL WINAPI InternetReadFile(
IN HINTERNET hFile,
IN LPVOID lpBuffer,
IN DWORD dwNumberOfBytesToRead,
OUT LPDWORD lpdwNumberOfBytesRead);
参数介绍:
Parameters
-
hFile
- Valid handle returned from a previous call to InternetOpenUrl, FtpOpenFile, or HttpOpenRequest. lpBuffer
- Long pointer to a buffer that receives the data read. 用来存下载的数据的缓冲区(一般为char 数组) dwNumberOfBytesToRead
- Specifies the number of bytes to read. 本次要读取的数据的长度(以字节为单位) lpdwNumberOfBytesRead
- Long pointer to a variable that receives the number of bytes read. InternetReadFile sets this value to zero before doing any work or error checking. 本次实际上读取的数据的长度(以字节为单位)
Return Values
TRUE indicates success. FALSE indicates failure. To get extended error information, call GetLastError. An application can also use InternetGetLastResponseInfo when necessary.
写个调用的上下文放这吧:
char* CGprsNet::GetContent(HINTERNET hHttpFile,unsigned int &iContentLen)
{
ASSERT(hHttpFile);
iContentLen = 0;
DWORD dwByteToRead = 0;
DWORD dwSizeOfRq = 4;
DWORD dwAck;
BOOL b = HttpQueryInfo(hHttpFile,HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,(LPVOID)&dwAck,&dwSizeOfRq,NULL);
if(!b||dwAck!=200)
{
WriteLog("HttpQueryInfo失败,err=",::GetLastError());
WriteLog("返回码:",dwAck);
return NULL;
}
b = HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,(LPVOID)&dwByteToRead,&dwSizeOfRq,NULL);
if(!b||dwByteToRead==0)
{
WriteLog("未能获得Content-Length!设定默认缓冲区大小为20KB/n");
dwByteToRead = 300*1024; //默认缓冲区大小
}
char* szTemp = new char[dwByteToRead];
dwSizeOfRq = 0;
do
{
if (!InternetReadFile(hHttpFile,szTemp+dwSizeOfRq,dwByteToRead-dwSizeOfRq,&dwAck))
{
delete[] szTemp; szTemp = NULL;
DWORD err = ::GetLastError();
WriteLog("InternetReadFile失败,错误码:",err);
return NULL;
}
if (!dwAck) break; // Condition of dwSize=0 indicate EOF. Stop.
dwSizeOfRq+=dwAck;
}while(true);
if(dwSizeOfRq == 0)
{
delete[] szTemp; szTemp = NULL;
WriteLog("警告:数据长度为0!/n");
return NULL;
}
iContentLen = dwSizeOfRq; //实际读取的数据长度
return szTemp;
}