C++ 关于断点续传的方法,使用CURL技术
来源:互联网 发布:cad线切割编程软件 编辑:程序博客网 时间:2024/05/25 16:38
STEP1
CURL *curlhandle = NULL;// curl_global_init(CURL_GLOBAL_ALL);curlhandle = curl_easy_init();bool flag = download(curlhandle,myvar->url.c_str(),myvar->path.c_str(),myvar->proxy_address.c_str(),myvar->proxy_port.c_str(),myvar->username.c_str(),myvar->password.c_str(),1,3);curl_easy_cleanup(curlhandle);curl_global_cleanup();
STEP2
int ABCD::download(CURL *curlhandle, const char * remotepath, const char * localpath, const char* proxy_address, const char* proxy_port, const char* username, const char* password, long timeout, long tries){ curl_off_t local_file_len = -1 ; long filesize =0 ; CURLcode res = CURLE_GOT_NOTHING; int c; struct stat file_info; int use_resume = 0; /* 得到本地文件大小 */ //if(access(localpath,F_OK) ==0) if(stat(localpath, &file_info) == 0) { local_file_len = file_info.st_size; use_resume = 1; } //采用追加方式打开文件,便于实现文件断点续传工作 fp = fopen(localpath, "ab+"); if (fp == NULL) { perror(NULL); return 0; } curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath); //setting proxy network ++++++std::string str_proxy="";str_proxy = proxy_address;str_proxy +=":";str_proxy += proxy_port; curl_easy_setopt(curlhandle, CURLOPT_PROXY, str_proxy.c_str()); //test OKstd::string str_user_pwd="";str_user_pwd = username;str_user_pwd +=":";str_user_pwd += password;curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, str_user_pwd.c_str()); //---------------------------------------------------------------------- curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout); // 设置连接超时,单位秒 //设置http 头部处理函数 curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc); curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize); // 设置文件续传的位置给libcurl curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0); curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);curl_easy_setopt(curlhandle, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curlhandle, CURLOPT_FOLLOWLOCATION, 1L);curl_easy_setopt(curlhandle, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curlhandle, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 0);curl_easy_setopt(curlhandle, CURLOPT_PROGRESSFUNCTION, my_progress_func);curl_easy_setopt(curlhandle, CURLOPT_PROGRESSDATA, iPer); res = curl_easy_perform(curlhandle); fclose(fp); if (res == CURLE_OK) {long http_code = 0;res = curl_easy_getinfo(curlhandle, CURLINFO_RESPONSE_CODE, &http_code);if(CURLE_OK == res && 200 == http_code){res = CURLE_OK;return 1;}else{res = CURL_LAST;//delete file , because the file is not existed!remove(localpath);return 0; // is failed! not find the download file.} } else { fprintf(stderr, "%s\n", curl_easy_strerror(res)); g_i_connect_network = 6; return 0; }}
STEP3
关于多线程如何传递多个值,请参考上一篇博客。
STEP4
需要定义一个结构体为传多value
static FILE *fp;static int iPer; //Define sturct typedef struct DOWNLOAD_PARAMETER{int index;std::string url; }Download_Parametar;
TEST OK!
0 0
- C++ 关于断点续传的方法,使用CURL技术
- 使用CURL断点续传时遇到的数据类型问题
- 关于使用curl 调用其他工程方法
- CURL断点续传
- CURL使用HTTPS的技术小结
- CURL使用HTTPS的技术小结
- PHP中关于cURL的使用
- 使用CURL来获取COOKIE的方法
- 关于使用ASIHTTPRequest进行断点续传
- C/C++使用Curl
- Cocos2d-3.10 使用Curl异步下载文件(断点续传)
- CURL断点续传(FTP/HTTP)
- 关于curl返回数据乱码的问题解决方法
- php用curl下载的断点续传实现方式
- 跨平台的文件上传下载断点续传工具CURL
- 关于linux下的curl命令的使用
- 关于curl和json的安装和使用
- 关于http协议的描述和curl使用详解
- GNU对C语言的扩展---结构体
- 珍惜着美好的时光
- [Java][MyBatis]mapperLocations属性通配符的使用
- 在iOS开发中自动获取当前的位置(GPS定位)
- 用户配置文件状态变成“备份”
- C++ 关于断点续传的方法,使用CURL技术
- HDOJ_就这么个烂题总是WA先放这把
- Ubuntu 12.10环境下Mochiweb安装
- R语言中的一些基本函数
- linux 路由表 的一些相关资料
- 菲绑匪索要7000万赎金,该不该给?
- 电视接口的AV,S端子,色差,VGA,DVI区别
- 是的感觉阿桑地方
- c++ static 关键字