windows下C语言使用curl库访问HTTP下载文件

【摘要】 curl是一个利用url语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。curl还包含了用于程序开发的libcurl。
一、前言 curl是一个利用url语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。curl还包含了用于程序开发的libcurl。
curl支持的通信协议有ftp、ftps、http、https、tftp、sftp、gopher、scp、telnet、dict、file、ldap、ldaps、imap、pop3、smtp和rtsp。
curl还支持ssl认证、http post、http put、ftp上传, http form based upload、proxies、http/2、cookies、用户名+密码认证(basic, plain, digest, cram-md5, ntlm, negotiate and kerberos)、file transfer resume、proxy tunneling。
二、curl下载 curl for windows : https://curl.se/windows/
下载页面如图:
解压后的可执行文件位置:
下面是解压后的文件目录:
在命令行使用curl测试下载文件:
三、通过命令行使用curl curl可以直接调用函数库完成功能设计、也可以直接调用可执行文件完成需要的功能,下面这里就介绍,在windows下,通过createprocess调用curl命令函数完成文件下载。使用curl实现http协议文件下载成功,通过给定的连接地址,可以完成文件下载,百分比进度返回等等。
/**************************************************作者: ds小龙哥功能: 执行命令参数解释:callbackfunction_p func_p :回调函数,用于通知进度执行过程char *text //进度的转码过程,详细描述.描述当前这个操作是做什么.char *total_time //执行的总时间char *cmd //执行的命令**************************************************/int file_down_func(callbackfunction_p func_p, const char *text, const char *total_time, const char *cmd){ bool run_pipe; process_information pi; startupinfo si; bool ret = false; dword flags = create_no_window; char pbuffer[210]; security_attributes sa; sa.nlength = sizeof(security_attributes); sa.lpsecuritydescriptor = null; sa.binherithandle = true; handle hreadpipe, hwritepipe; run_pipe = createpipe(&hreadpipe, &hwritepipe, &sa, 0); if (run_pipe != 1) { printf(创建匿名管道文件失败=%d\n, run_pipe); return -1; } zeromemory(&pi, sizeof(process_information)); zeromemory(&si, sizeof(startupinfo)); si.cb = sizeof(startupinfo); si.dwflags |= startf_usestdhandles; si.hstdinput = null; si.hstderror = hwritepipe; si.hstdoutput = hwritepipe; wchar_t cmd_wchar[1024]; chartowchar(cmd, cmd_wchar); //tchar cmd[] = text(ffmpeg -i d:\\123.mp4 -vf reverse d:\\out\\out1.mp4); ret = createprocess(null, cmd_wchar, null, null, true, flags, null, null, &si, &pi); if (ret) { while (true) { dword exitcode = 0; //判断进程是否执行结束 printf(正在执行...getexitcodeprocess\r\n); getexitcodeprocess(pi.hprocess, &exitcode); printf(exitcode:%d\r\n, exitcode); if (exitcode == still_active) //正在运行 { dword rsize = 0; bool run_s = 0; printf(正在执行...readfile\r\n); run_s = readfile(hreadpipe, pbuffer, sizeof(pbuffer), &rsize, null); pbuffer[rsize - 1] = '\0'; printf(执行过程:%s,%d,%d,%s\n, version_str, run_s, rsize, pbuffer); char number_buff[10]=\0; //存放百分比 printf(pbuffer=%s\r\n, pbuffer); //通过回调函数向外部返回进度提示 for (size_t i = 0; i = '0' && pbuffer[i] <= '9') { //得到百分比值 for (size_t j = 0; j = '0' && pbuffer[i+j] 0) { std::string out_str; out_str = text; out_str += ,; out_str += 100; out_str += ,; out_str += number_buff; printf(回调:%s\r\n, out_str.c_str()); //将执行的结果再回调出去 if (func_p) { func_p(out_str.c_str()); } } } else //结束 { printf(执行完毕,exitcode=%d\r\n, exitcode); break; } } printf(正在等待子进程结束....\n); //等待结束 waitforsingleobject(pi.hprocess, infinite); closehandle(pi.hprocess); closehandle(pi.hthread); printf(子进程执行完毕....\n); return 0; } printf(子进程创建失败:%d\n, ret); return -1;}int main(){ string videocachefilepath = d:\\out; //下载的文件名称 string file_path = http://192.168.1.110:8001/uploads/1/哈哈哈.mp4; //如果返回为真就表示是网络地址 if (strstr(file_path.c_str(), http:) || strstr(file_path.c_str(), https:)) { //1.获取不带路径的文件名 string::size_type ipos; if (strstr(file_path.c_str(), \\)) { ipos = file_path.find_last_of('\\') + 1; } else { ipos = file_path.find_last_of('/') + 1; } //得到文件名称 string base_file = file_path.substr(ipos, file_path.length() - ipos); //得到完整的文件下载存储路径 string videopath_tmp = videocachefilepath; videopath_tmp += \\; videopath_tmp += base_file; //判断文件是否存在 printf(文件存储路径:%s\r\n, videopath_tmp.c_str()); file *file_p = fopen(videopath_tmp.c_str(),rb); //不存在就下载 if (file_p == nullptr) { //切换目录,进入到curl命令所在的目录 _chdir(videocachefilepath.c_str()); string cmd = curl -o ; cmd += file_path; //启动下载文件 file_down_func(null, base_file.c_str(),100,cmd.c_str()); } else { printf(文件存在不需要下载.\r\n); fclose(file_p); } } return 0;}
家庭光伏系统的优缺点
面向高速铣削的加工策略 -CimatronE与高速铣
短视频社交,终于揭开了腾讯的短板
手势识别:解放双手,开启智能时代
专家开讲:恩智浦车载网络处理器S32G3,及其在Zonal架构中的最新实践
windows下C语言使用curl库访问HTTP下载文件
特斯拉Model 3将扩大到巴基斯坦控制污染
编程语言排行榜:C#快取代C++的第三名
英创信息技术JDA901 6网口单板工控机简介
EP7209 ARM单芯片系统功能特征及其嵌入式应用
N沟道结型场效应管的结构
RFaxis扩大RFX1010宽带Sub-GHz CMOS RFeIC产能
对音频放大器咔嗒声的定量分析
小米Mix2百分百屏占比! “买吗? ” 网友: “不买! ”
基于C6000系列DSP的MPEG
AI技术应该受到人类智能的启发
JL-700W Zhaga Book-18锁扣式插座解读
存储器迎来怎样的2023?
年销售收入12亿!我国北方最大机器人基地在青岛高新区开建
team上线“远程同乐”功能 支持非对称VR内容