下载是下载成功了,但是文件大小只有321字节,原文件大小为3.7M,郁闷啊,不知道是怎么回事!求助~~~
CHttpConnection *m_pHttp;
CInternetSession m_cis;
DWORD m_dwHttpRequestFlags;
m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
CString strfname;
strFile="http://192.168.2.7/2.bmp";
m_pHttp=m_cis.GetHttpConnection(strFile,80);
m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
CHttpFile *pFile =m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET, 
strFile, NULL, 1, NULL, NULL, m_dwHttpRequestFlags);
CStdioFile csf;
CString str;
str="E:\2.bmp";
csf.Open(str,CFile::modeCreate|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite);
char buf[MAX_PATH];
while((n=pFile->Read(buf,sizeof(buf)))>0)
{
csf.Write(buf,n);
}
pFile->Close();

解决方案 »

  1.   

    如果用URLDownloadToFile这个就可以,为什么我用我写的那段代码就不行呢?
      

  2.   

    ((n=pFile->Read(buf,sizeof(buf)))>0)sizeof(buf) 是 buf 指针长度,并不是buf的实际长度. 应该是((n=pFile->Read(buf,MAX_PATH))>0)
      

  3.   

    BOOL bResult = FALSE; CInternetSession* pSession = new CInternetSession();
    CHttpFile* pFile = NULL;
    CFile localFile;
    if (!localFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite, NULL))
    {
    return FALSE;
    } try
    {
    char buf[128 * 1024]; pFile = (CHttpFile*)pSession->OpenURL(strURL);
            int iLen = pFile->Read(buf, 128 * 1024);
    localFile.Write(buf, iLen);

    pFile->Close(); bResult = TRUE;
    }
    catch (...)
    {
    bResult = FALSE;
    } localFile.Close();
    pSession->Close(); if (pFile != NULL)    delete pFile;
    if (pSession != NULL) delete pSession; return bResult;
      

  4.   

    char buf[1024];
    while((n=pFile->Read(buf,1024))>0)
    {
    TRACE("%d\n",n);
    csf.Write(buf,n);
    }看下是怎么接收的  断在什么地方.
      

  5.   

    TO newone2007 你的方法还是不行TO sangermax 获取了n但是它的长度只有321
      

  6.   

    TO jinghao666666 文件属性要改成什么样的?
      

  7.   

    没有满意的答案,我只能用URLDownloadToFile这个了,唉!