• C++通过http下载二进制文件


    最近做了个动态库的升级程序,需要到服务器检查是否有新版本的动态库如果有的话就下载下来升级。这里与大家分享我的测试下载该二进制文件的过程。如下:

    #include <stdio.h>
    #include <windows.h>
    #include <wininet.h>
    #define MAXBLOCKSIZE 1024

    void download(const char*);

    int main(int argc, char* argv[]){
     if(argc > 1){
      download((const char*)argv[1]);
     }else{
      printf("Usage: auto-Update url");
     }
     return 0;
    }


    /**
     * 执行 文件下载 操作
     * @param Url: The target action url
     *
     */
    void download(const char *Url)
    {
     HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if (hSession != NULL)
     {
      HINTERNET handle2 = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
      if (handle2 != NULL)
      {
       printf("%s/n",Url);
       byte Temp[MAXBLOCKSIZE];
       ULONG Number = 1;

       FILE *stream;
       if( (stream = fopen( "F://LeadBridge//new.exe", "wb" )) != NULL )//这里只是个测试,因此写了个死的文件路径
       {
        while (Number > 0)
        {
         InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
         //fprintf(stream, (const char*)Temp);
         fwrite(Temp, sizeof (char), Number , stream);
        }
        fclose( stream );
       }
      
       InternetCloseHandle(handle2);
       handle2 = NULL;
      }
      InternetCloseHandle(hSession);
      hSession = NULL;
     }
    }

  • 相关阅读:
    pygame中的图像和音乐
    pygame中鼠标画直线
    Python-GUI:button及entry的应用
    Linux查看物理CPU个数、核数、逻辑CPU个数
    阿里云nas使用记录
    字符集错误解决
    firewalld防火墙命令规则设置
    Linux之TCPIP内核参数
    nginx 301跳转https后post请求失效问题解决
    TCP queue 的一些问题
  • 原文地址:https://www.cnblogs.com/chuncn/p/1411202.html
Copyright © 2020-2023  润新知