HTTP下载文件

  • 时间:
  • 来源:互联网
  • 文章标签:
bool CWinpcapGetURL::DownLoadWBList(LPCSTR ServerName, LPCSTR lpObject, int nPort)//下载黑白名单
{
    HINTERNET hSession = NULL;
    HINTERNET hConnect = NULL;
    
    DWORD dwBytesRead = 0;
    char * pBuffer;
    FILE* hWDListFile;
    HINTERNET hRequest=NULL;
    
    hSession = InternetOpen("HttpSendRequest",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    if(!hSession){
        goto quit;
    }
    
    hConnect = InternetConnect(hSession, ServerName, nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    if(!hConnect)
    {
        goto quit;
    }
    
    hRequest = HttpOpenRequest (hConnect, "GET", lpObject, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
    if (!hRequest)
    {
        goto quit;
    }
    
    {        
        BOOL bSendRequest = ::HttpSendRequest(hRequest,   NULL,   0,   0,   0);   
        
        //Get the length of the file.   
        char bufQuery[32];   
        DWORD dwLengthBufQuery = sizeof(bufQuery);   
        BOOL bQuery = ::HttpQueryInfo(hRequest,   HTTP_QUERY_CONTENT_LENGTH,bufQuery,&dwLengthBufQuery,NULL);         
        DWORD dwFileSize = (DWORD)atol(bufQuery);   
        
        pBuffer = new char[dwFileSize+1]; 
        if(!InternetReadFile(hRequest, pBuffer, dwFileSize+1, &dwBytesRead))
        {
            goto quit;
        }
        if( dwBytesRead == 0)
            goto quit;
        
        pBuffer[dwBytesRead] = 0;
        string s_WDListFile = CGlobalTools::g_szCurrentDirectory;
        s_WDListFile.append("WBListCache.bak");
        hWDListFile = fopen(s_WDListFile.c_str(),"w+");
        
        DWORD dWriteSize = fwrite(pBuffer,1,dwBytesRead,hWDListFile);
    }
    
quit:
    if(hSession)
        InternetCloseHandle(hSession);
    if(hRequest)
        InternetCloseHandle(hRequest);
    if (hConnect)
        InternetCloseHandle(hConnect);    
    if (hWDListFile != NULL )
        fclose(hWDListFile);    
    if (pBuffer)
    {
        delete[] pBuffer;
        pBuffer = NULL;
    }
    return true;
}

 

本文链接http://www.taodudu.cc/news/show-647709.html