下面是我写的一个实现HTTP文件下载的函数,采用直连方式。如何让它支持HTTP代理?是修改修改某些参数?还是干脆采用别的方式?
BOOL HeslNetOper::HttpFileDown(CString& strDownURL,CString& strLocalSavePath)
{
CInternetSession lInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
//1、打开下载文件
CHttpFile* pHttpFile=NULL;
try
{
pHttpFile=(CHttpFile*)lInternetSession.OpenURL(strDownURL); DWORD dwRet;
pHttpFile->QueryInfoStatusCode(dwRet);
}
catch(CInternetException* e)
{
char szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
pHttpFile=NULL; return FALSE;
}
//2、保存下载文件
if(pHttpFile)
{
//打开本地文件
CStdioFile lStdioFile;
lStdioFile.Open(strLocalSavePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
//向本地文件中写入数据
char chrBuffer[2048];
DWORD dwBytesRead;
while((dwBytesRead=pHttpFile->Read(chrBuffer,2048))>0)
{
lStdioFile.Write(chrBuffer,dwBytesRead);
} //资源回收
lStdioFile.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpFile=NULL;
}
}

解决方案 »

  1.   

    DWORD GetHttpFileByUrl(CString& strUrl,CString& strFileText)
    {
    char szTextBuffer[1024];
    memset(szTextBuffer,0,sizeof(szTextBuffer));    CInternetSession NetSession;
    CHttpFile *pFile = NULL; 

    DWORD dwReadBytes = 0;

    TRY
    {
    CString strServer;
    CString strObject;
    DWORD dwServiceType;
    INTERNET_PORT nServerPort;

    if(!AfxParseURL(strUrl,dwServiceType,strServer,strObject,nServerPort))
    {
    return 1;
    //URL address is invalid.
    }

    CHttpConnection *pHttpConnection = NetSession.GetHttpConnection(strServer,nServerPort,NULL,NULL);
    if(pHttpConnection == NULL) //Network net connection.
    {
    return 5;
    }

    RE_SEND_HTTP_REQUEST: pFile = pHttpConnection->OpenRequest(
    NULL, strObject, NULL, 1, NULL, NULL,
    INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION
    );
    if(pFile==NULL)
    {
    NetSession.Close();
    return 10;
    }

    //AfxMessageBox("SendRequest");
    if(!pFile->SendRequest(NULL))
    {
    NetSession.Close();
    return 15;
    }
    DWORD dwStatusCode;
    if(!pFile->QueryInfoStatusCode(dwStatusCode))
    {
    NetSession.Close();
    return 20;
    } if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ || dwStatusCode == HTTP_STATUS_DENIED)
    {
    char szData[51];
    DWORD dwSize;
    //may read error info of the file to szData
    do
    {
    //::InternetReadFile(m_hHttpFile, (LPVOID)szData, 50, &dwSize);
    dwSize = pFile->Read(&szData,50);
    }
    while (dwSize != 0);

    if(!pFile->ErrorDlg(
    NULL,ERROR_INTERNET_INCORRECT_PASSWORD,
    FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
    FLAGS_ERROR_UI_FLAGS_GENERATE_DATA|
    FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
    NULL)
    )
    {
    NetSession.Close();
    return 25;
    } //until dwStatusCode==HTTP_STATUS_OK we can do next
    goto RE_SEND_HTTP_REQUEST;

    }
    else
    {
    if(dwStatusCode!=HTTP_STATUS_OK)
    {
    //NetSession.Close();
    //return 30;
    }
    } //?áè?êy?Yμ?szTextBuffer
    DWORD dwLength = pFile->GetLength();
    if(dwLength < sizeof(szTextBuffer))
    {
    dwReadBytes = pFile->Read(szTextBuffer, dwLength);
    }
    else
    {
    dwReadBytes = pFile->Read(szTextBuffer, sizeof(szTextBuffer)-1);   //?á·μ??μ??á1?
    }
            
    strFileText = szTextBuffer;
    pFile->Close();
    NetSession.Close();
    }
    CATCH(CInternetException , pEr)
    {
    //pEr->ReportError();
    return 2;
    }
        CATCH(CFileException , pEr)
    {
    //pEr->ReportError();
    return 3;
    }
    END_CATCH return 0;
    }