zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    hadoop 2.x 简单实现wordCount
    httpClient连接超时设置
    Java io使用简介
    log4j使用教程
    F#中的自定义隐式转换
    Computation expressions and wrapper types
    Introducing 'bind'
    Understanding continuations
    Computation expressions: Introduction
    MySQL优化总结,百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/tangtianfly/p/2499367.html
Copyright © 2011-2022 走看看