C++ wininet和winsock下载文件

C++下载文件可以通过wininet或socket方式实现。

1.winnet方式(如果在VS05或VS08中编译,需要设置:项目-》属性-》配置属性-》常规-》字符集-》如果是Unicode,则设置成“使用多字节字符集”)

CVodStreamCache.h

  1.  
  2. #ifndef _CVodStreamCache_H
  3. #define _CVodStreamCache_H
  4.  
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. using namespace std;
  11.  
  12. #pragma comment(lib, "wininet.lib")
  13.  
  14.  
  15. class CVodStreamCache
  16. {
  17. public:
  18.     HANDLE Create(char token[], char path[]);
  19.     int ReadBlock(long offset, int len, char *buffer);
  20.     int WriteBlock();
  21.     void Destory();
  22.     void CacheInfo();
  23. protected:
  24. private:
  25.     HINTERNET internetOpen;
  26.     HINTERNET internetOpenUrl;
  27.  
  28.     DWORD dwStatusCode;
  29.     DWORD dwContentLength;
  30.  
  31.     HANDLE createFile;
  32.     static const int BUFFERSIZE = 1024;
  33. };
  34.  
  35.  
  36. #endif

CVodStreamCache.cpp

  1.  
  2. #include "CVodStreamCache.h"
  3.  
  4. HANDLE CVodStreamCache::Create(char token[], char path[])
  5. {
  6.     internetOpen = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  7.     if (internetOpen == NULL)
  8.     {
  9.         cout << "Internet open failed!" << endl;
  10.         return NULL;
  11.     }
  12.  
  13.     //LPCTSTR p = TEXT(path);
  14.     internetOpenUrl = InternetOpenUrl(internetOpen, path,
  15.         NULL, 0, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_PRAGMA_NOCACHE, 0);
  16.     if (internetOpenUrl == NULL)
  17.     {
  18.         cout << "Internet open url failed! Error code = " << GetLastError() << endl;
  19.         InternetCloseHandle(internetOpen);
  20.         return NULL;
  21.     }
  22.  
  23.     DWORD dwStatusSize = sizeof(dwStatusCode);
  24.     HttpQueryInfo(internetOpenUrl, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwStatusSize, NULL);
  25.     cout << "Status: " << dwStatusCode << endl;
  26.  
  27.     DWORD dwLengthSize = sizeof(dwContentLength);
  28.     HttpQueryInfo(internetOpenUrl, HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER, &dwContentLength, &dwLengthSize, NULL);
  29.     cout << "File size: " << dwContentLength << endl;
  30.  
  31.     createFile = CreateFile(_T("E://download.jpg"), GENERIC_WRITE|GENERIC_READ, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  32.     if (createFile == INVALID_HANDLE_VALUE)
  33.     {
  34.         cout << "Create file failed!" << endl;
  35.         InternetCloseHandle(internetOpenUrl);
  36.         return NULL;
  37.     }
  38.  
  39.     return createFile;
  40. }
  41.  
  42. int CVodStreamCache::ReadBlock(long offset, int len, char *buffer)
  43. {
  44.     return 0;
  45. }
  46.  
  47. int CVodStreamCache::WriteBlock()
  48. {
  49.     BOOL internetReadFile;
  50.     char buffer[BUFFERSIZE];
  51.     memset(buffer, 0, sizeof(buffer));
  52.     DWORD byteRead = 0;
  53.  
  54.     BOOL hwrite;
  55.     DWORD written;
  56.  
  57.     DWORD byteDown = 0;
  58.     int a = 0;
  59.     cout << "已下载:" << a << "%";
  60.     while (1)
  61.     {
  62.         internetReadFile = InternetReadFile(internetOpenUrl, buffer, sizeof(buffer), &byteRead);
  63.         if (byteRead == 0)
  64.         {
  65.             break;
  66.         }
  67.  
  68.         byteDown += byteRead;
  69.         cout << "/b";
  70.         do
  71.         {
  72.             cout << "/b";
  73.             a /= 10;
  74.         } while (a);
  75.         a = 100 * byteDown / dwContentLength;
  76.         cout << a << "%";
  77.  
  78.         //SetFilePointer(createFile, a, NULL, FILE_BEGIN);
  79.         hwrite = WriteFile(createFile, buffer, sizeof(buffer), &written, NU
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值