Http服务器下载文件

需要包含的头文件和库文件

#include <string>

#include <Wininet.h>

#include <fstream>

#pragma comment(lib,"Wininet.lib")

 

1.打开服务器

    int CHTTP::CreateConnect(const string & HTTPServer,const string & UserName,const string &Password,const int nPort)

    {

        DWORD err;
        m_Session = NULL;
        m_Connect = NULL;

        HINTERNET m_Session = InternetOpen(L"AGENT",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);

        if(m_Session != NULL)

       {

            if (HTTPServer.find("www")!=string::npos)
           {
                m_Connect = InternetConnectA(m_Session, HTTPServer.c_str(),nPort,UserName.c_str(),Password.c_str(), INTERNET_SERVICE_HTTP,0,0); 
           }
          else
          {
               m_Connect = InternetConnectA(m_Session, HTTPServer.c_str(),INTERNET_DEFAULT_HTTP_PORT,UserName.c_str(),Password.c_str(), INTERNET_SERVICE_HTTP,0,0);
          }

      }    

      if(m_Connect == NULL)
      {
           err = GetLastError();

           return OP_FAIL;
      } 

      return OP_SUCCESS;
   }

2.下载文件

    int CHTTP::DownloadFile(const wstring & RemoteFileName,const wstring & LocalPath,const wstring & LocalFileName)
    {
        if(m_Connect == NULL)
        {
            return OP_FAIL;
        }
 
        HINTERNET m_WebSite = HttpOpenRequest(m_Connect,L"GET",RemoteFileName.c_str(),HTTP_VERSION,NULL,0,INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD,0);


        if(m_WebSite == NULL)
        {
             DWORD err = GetLastError();

             return OP_FAIL;
        }

        BOOL   bSendRequest=::HttpSendRequest(m_WebSite,NULL,0,0,0); 

        wchar_t   achQueryBuf[1024];   

        DWORD   dwQueryBufLen = sizeof(achQueryBuf);  

        BOOL   bQuery=::HttpQueryInfo(m_WebSite,HTTP_QUERY_STATUS_CODE,achQueryBuf,&dwQueryBufLen,NULL);
 
        wstring str = achQueryBuf;

        if (m_pInitStruct != NULL)
       {
           wchar_t   QueryBuf[1024];   
           DWORD     QueryBufLen = sizeof(QueryBuf);
           BOOL   bLQuery=::HttpQueryInfo(m_WebSite,HTTP_QUERY_CONTENT_LENGTH,QueryBuf,&QueryBufLen,NULL);

           wstring Buff = QueryBuf;
           int length = _wtoi(Buff.c_str());
       }
       if(!bQuery)
       { 

           return OP_FAIL;
       }

      if(str == L"200" || str == L"302")
      {
          //临时文件名

         wstring TempFile = LocalPath+L\\package.tmp;

         DeleteFile(TempFile.c_str());

         FILE     *fp_File = NULL;
         DWORD err = _wfopen_s(&fp_File,TempFile.c_str(), L"wb");

         DWORD Number = 1;

         DWORD MAXBLOCKSIZE = 512;
         char Temp[512];

         while(Number)
         {
             memset(Temp,0,512);

             if (!InternetReadFile(m_WebSite,Temp,MAXBLOCKSIZE-1,&Number))
             {
                 fclose(fp_File);
                 return OP_FAIL;
             }
    
             fwrite(Temp, sizeof(char), Number, fp_File);
          }
  
          fclose(fp_File);

          //重命名为目标文件
          wstring DesFile =  LocalPath;
     
          DeleteFile(DesFile.c_str());
     
          ::MoveFile(TempFile.c_str(),DesFile.c_str());

          return OP_SUCCESS;
      }
      else
      {
          return OP_FAIL;
      }
   }

3.关闭服务器  

    int CHTTP::Close()
    {
        if(m_WebSite != NULL)
        {
            InternetCloseHandle(m_WebSite);  
        }
        if(m_Connect != NULL)
        {
             InternetCloseHandle(m_Connect);  
        } 

        if(m_Session != NULL)
       {
            InternetCloseHandle(m_Session);  
       }

       return OP_SUCCESS; 
    }

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。