我写一个到HTTP上下载文件的程序,因为我要做进度条,所以我必须知道要下载文件的总大小
我用HTTPFILE.GETLENGTH();有些文件可以得到正确的数,但有一部分有得不到正确的数,我把代码
贴出来吧,请高手帮我看看是什么回事啊,,谢了
                  CInternetSession session;
                   session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,10);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
        CHttpConnection *pConn=session.GetHttpConnection("www.lianlian.com.cn");
CHttpFile *hFile = NULL;
hFile = pConn->OpenRequest("GET", "/download/Tool.exe"); if(hFile)
{
hFile->SendRequest();
char *m_pBuffer;
            m_pBuffer=(char*)malloc(sizeof(char)*1024*1024*2);  // m_pBuffer中保存下载文件的内容
long m_iBuffer = 0;//1024;
int iBytes = 0;
long iLength=hFile->GetLength();
long lCurrentPos=0;
float fPer=0;
CFile SaveFile;
CIniFile IniFile;
SaveFile.Open(IniFile.GetModulePath()+"UpdatePro.exe",CFile::modeCreate|CFile::modeWrite|CFile::modeRead,NULL);
while((iBytes = hFile->Read(m_pBuffer+m_iBuffer,1024)) )
{
m_iBuffer += iBytes;
lCurrentPos+=iBytes;
if(m_iBuffer>1024*1023*2)
{
SaveFile.SeekToEnd();
SaveFile.Write(m_pBuffer,m_iBuffer);
m_iBuffer=0;
}
fPer=1.0*lCurrentPos/iLength;
m_Progress1.SetPos(fPer*100);
}



SaveFile.SeekToEnd();
SaveFile.Write(m_pBuffer,m_iBuffer);
SaveFile.Close();
hFile->Close();
pConn->Close();

free(m_pBuffer); session.Close();
MessageBox("下载完毕!");

}
else
{
return ;
}

解决方案 »

  1.   

    怎么说话都说那么点点啊,怎么都不说完啊,,,GET命令怎么弄,,wininet 提供由现成函数怎么调用??
      

  2.   

    Stream input=result.GetResponseStream();
      

  3.   

    hFile->GetLength();
    是得不到数据的长度的
    可以用QueryInfo来代替
    pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,strInfo);
      

  4.   

    to ncsy1983 (游客) 借贵宝一用,谢谢。为什么只能下载扩展名htm,txt,exe的文件,不能下载其它的文件如扩展名为dll的文件呢?
    http://community.csdn.net/Expert/topic/3909/3909257.xml?temp=.3922083
    这是我问的贴子,我已经不能回贴了,所以来到你这里,谢谢。
      

  5.   

    自己GET一下就知道了,怎么会没有长度呢!