• libcurl下载文件简易demo


    size_t test_save(void *ptr, size_t size, size_t nmemb, void *stream)
    {
    	size_t sizes = size * nmemb;
    	DWORD dwHasWritten;
    	WriteFile((HANDLE)stream,ptr,sizes,&dwHasWritten,NULL);
    	return sizes;
    }
    
    int test_get(const char *url, HANDLE hFile)
    {
    	CURL *pCurl = curl_easy_init();
    	int responseCode = -1;
    	CURLcode returnCode = CURLE_OK;
    	do
    	{
    		if (!pCurl)
    		{
    			break;
    		}
    		returnCode = curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10); //设置连接超时时间
    		CURL_BREAK_IF_NOT_OK(returnCode);
    
    		returnCode = curl_easy_setopt(pCurl, CURLOPT_URL, url);              //设置请求链接
    		CURL_BREAK_IF_NOT_OK(returnCode);
    
    		returnCode = curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, hFile);      //设置CURLOPT_WRITEFUNCTION的最后一个参数值
    		CURL_BREAK_IF_NOT_OK(returnCode);
    
    		returnCode = curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, test_save); //设置接收到数据之后的回调函数
    		CURL_BREAK_IF_NOT_OK(returnCode);
    
    		returnCode = curl_easy_perform(pCurl);                                    //开始传输
    		CURL_BREAK_IF_NOT_OK(returnCode);
    
    		returnCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &responseCode); //获取返回信息
    	} while (0);
    	if (pCurl)
    	{
    		curl_easy_cleanup(pCurl);
    	}
    	return responseCode;
    }
    
    	HANDLE hFile = CreateFileW(L"d:\22.exe", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ/*|FILE_SHARE_DELETE*/, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (INVALID_HANDLE_VALUE != hFile)
    	{
    		test_get("http://dldir1.qq.com/qqfile/qq/QQ8.4/18380/QQ8.4.exe", hFile);
    	}
    	CloseHandle(hFile);
    	hFile = NULL;
    
  • 相关阅读:
    Redis设计与实现第一部分:第5章:Redis 跳跃表
    根据临时表修改主表的某字段数据根据主表的主键
    Redis设计与实现第一部分:第2章:简单动态字符串SDS
    Redis
    MySQL的访问控制与用户管理
    MySQL字符集和语言的基础知识
    生成日志文件
    Python进阶09 动态类型
    Python进阶08 异常处理
    Python进阶07 函数对象
  • 原文地址:https://www.cnblogs.com/ultracpp/p/5707812.html
Copyright © 2020-2023  润新知