利用MFC的Wininet以http方式下载小文件

简介:

直接上函数代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <afxwin.h>         // MFC 核心组件和标准组件
#include <Wininet.h>
#include <iostream>
#include <string>
using  namespace  std;
//walker 2013.05
//hostname主机名,例如www.baidu.com
//suburl,例如asp/test.asp?num=3
//port,端口号
string GetHttpFile( const  char  hostname[],  const  char  suburl[], unsigned  short  port)
{
     HINTERNET   hInternet = NULL,
         hConnect = NULL,
         hRequest = NULL;
     BOOL  bRtn;
     string strResponse;
     hInternet = InternetOpen( "User-Agent" ,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL, 0 );
     if  (NULL == hInternet)
     {
         cout <<  "InternetOpen Failed!"  << endl;
         goto  exception_over;
     }
     hConnect = InternetConnect(hInternet, hostname, port, NULL,  " HTTP/1.1 "  ,INTERNET_SERVICE_HTTP, 0 , 0 );
     if  (NULL == hConnect)
     {
         cout <<  "InternetConnect Failed!"  << endl;
         goto  exception_over;
     }
     hRequest  =  HttpOpenRequest(hConnect,  "GET" , suburl,  " HTTP/1.1 "  ,NULL,NULL,INTERNET_FLAG_RELOAD, 0 );
     if  (NULL == hRequest)
     {
         cout <<  "HttpOpenRequest Failed!"  << endl;
         goto  exception_over;
     }
     //三种超时值
     DWORD  TimeOuts[] = {5*1000, 5*1000, 10*1000};
     InternetSetOption(hRequest,INTERNET_OPTION_CONNECT_TIMEOUT , &(TimeOuts[0]),  sizeof ( DWORD ));
     InternetSetOption(hRequest,INTERNET_OPTION_SEND_TIMEOUT , &(TimeOuts[1]),  sizeof ( DWORD ));
     InternetSetOption(hRequest,INTERNET_OPTION_RECEIVE_TIMEOUT, &(TimeOuts[2]),  sizeof ( DWORD ));
     bRtn  =  HttpSendRequest(hRequest,NULL, 0 , NULL, 0);
     if  (!bRtn)
     {
         cout <<  "HttpSendRequest Failed!"  << endl;
         goto  exception_over;
     }
     char  buf[1000] = {0};
     DWORD  dwLengthBufQuery =  sizeof (buf);
     bRtn = HttpQueryInfo(hRequest,  HTTP_QUERY_CONTENT_LENGTH, buf, &dwLengthBufQuery, NULL);
     if  (!bRtn)
     {
         cout <<  "HttpQueryInfo Failed!"  << endl;
         goto  exception_over;
     }
     int  file_len =  atoi (buf);
     cout <<  "filelen: "  << file_len <<  "bytes"  << endl;
     int  i = 1;
     const  int  BUF_LEN = 256;
     char  cReadBuffer[BUF_LEN] = {0};
     unsigned  long  sum_recv = 0;
     unsigned  long  read_len;
     while  (TRUE)
     {
         memset (cReadBuffer, 0 , BUF_LEN);
         unsigned  long   lNumberOfBytesRead;
         read_len = (file_len - sum_recv) > (BUF_LEN - 1) ? (BUF_LEN - 1) : (file_len - sum_recv);
         bRtn  =  InternetReadFile(hRequest, cReadBuffer, read_len , &lNumberOfBytesRead);
         //注意:这里认为文件中不会有'\0'
         strResponse  =  strResponse +  cReadBuffer;
         cout <<  "第"  << i <<  "次: "  << lNumberOfBytesRead <<  "bytes"  << endl;
         if  ( !bRtn  || strResponse.size() >= (unsigned  int )file_len)
         {
             break  ;
         }
         ++i;
     }
exception_over:
     if  (NULL != hRequest)
     {
         InternetCloseHandle(hRequest);
     }
     if  (NULL != hConnect)
     {
         InternetCloseHandle(hConnect);
     }
     if  (NULL != hInternet)
     {
         InternetCloseHandle(hInternet);
     }
     return  strResponse;
}


***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1206538如需转载请自行联系原作者


RQSLT

相关文章
|
5月前
Netty实战: HTTP文件列表服务器
Netty实战: HTTP文件列表服务器
57 0
|
5月前
|
Ubuntu Linux 时序数据库
|
2月前
|
机器学习/深度学习 Ubuntu Linux
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
|
2月前
|
传感器 机器学习/深度学习
如何下载DVS Gesture数据集?解决tonic.datasets.DVSGesture错误HTTP Error 403: Forbidden
本文介绍了如何解决在使用tonic库下载DVSGesture数据集时遇到的HTTP Error 403 Forbidden错误,建议从Figshare的链接下载完整数据集并解压到指定目录,以便成功加载数据集进行手势识别研究。
49 1
|
3月前
|
人工智能
AI绘画,Stable Diffusion如何使用中文简体包,黑色页面切换参数http://127.0.0.1:7860/?__theme=dark 两个__,中文包下载和安装
AI绘画,Stable Diffusion如何使用中文简体包,黑色页面切换参数http://127.0.0.1:7860/?__theme=dark 两个__,中文包下载和安装
|
4月前
|
移动开发 Java
Java Socket编程 - 基于Socket实现HTTP下载客户端
Java Socket编程 - 基于Socket实现HTTP下载客户端
32 1
|
4月前
|
JavaScript
vue : 无法加载文件 D:\module\npm_module\npm_modules\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.c
vue : 无法加载文件 D:\module\npm_module\npm_modules\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.c
|
5月前
|
数据采集 存储 数据安全/隐私保护
拓展网络技能:利用lua-http库下载www.linkedin.com信息的方法
本文介绍如何使用Lua和lua-http库抓取LinkedIn信息,强调了Lua在爬虫开发中的应用。通过配置亿牛云爬虫代理解决IP封锁问题,实现步骤包括安装库、配置代理、发送HTTP请求、解析响应及提取信息。提供的Lua代码示例展示了下载和存储LinkedIn信息的过程。实验成功展示了Lua爬虫的可行性,但也指出需考虑反爬虫策略以应对实际挑战。
拓展网络技能:利用lua-http库下载www.linkedin.com信息的方法
|
4月前
|
存储 运维 Serverless
Serverless 使用OOS将http文件转存到对象存储
阿里云OOS提供了一种高效、灵活的解决方案,用于自动化HTTP文件到对象存储的转存。通过OOS,用户可以使用函数计算FC执行Python脚本,直接将文件从HTTP源转移到OSS,无需本地存储或额外ECS实例,降低了成本,提高了效率,并减少了错误。实践步骤包括创建OOS模板并在FC上运行。使用此功能需开通FC服务,创建RAM角色并授权访问OSS。附录中提供了示例模板和Python脚本说明。
|
5月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
1063 1
下一篇
无影云桌面