curl http或https上传下载

2023-05-16

curl的使用

注意如果是https请求需要带上ssl的库和curl库如下图

在这里插入图片描述

上传文件

回调函数

static size_t http_data_writer(void* data, size_t size, size_t nmemb, void* content)
{
	long totalSize = size*nmemb;
	return totalSize;
}

formdata格式发送文件分块
lpData键值对;filename文件名称;size数据块大小;strData服务端返回值

bool Post(const char * lpUrl,const char *lpData,const char *filename,int size,string &strData)
{
	CURL *m_pCurl= curl_easy_init();
	struct curl_httppost *formpost = 0;
	struct curl_httppost *lastptr=NULL;
	curl_easy_setopt(m_pCurl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(m_pCurl, CURLOPT_URL, lpUrl);
	curl_easy_setopt(m_pCurl, CURLOPT_DEFAULT_PROTOCOL, "https");
	 
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "multipart/form-data;charset=UTF-8");
    curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, headers);
	//curl_easy_setopt(m_pCurl, CURLOPT_TIMEOUT,10);//等待超时时间

	curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, false);
	curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, false);

	//分块大小size 文件指针fp
     curl_formadd(&formpost,
        &lastptr,
                 CURLFORM_COPYNAME, "files",
                 CURLFORM_BUFFER, filename,
                 CURLFORM_BUFFERPTR, lpData,
                 CURLFORM_BUFFERLENGTH, size,
                 CURLFORM_CONTENTTYPE, "multipart/form-data",
                 CURLFORM_END);

     curl_formadd(&formpost,&lastptr,CURLFORM_CONTENTHEADER,headers,CURLFORM_END);
     curl_easy_setopt(m_pCurl, CURLOPT_HTTPPOST, formpost);
     
     curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, (void*)&strData);
     curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, http_data_writer);

	
    CURLcode res= curl_easy_perform(m_pCurl);
    if (res == CURLE_OK) {
		long scode = 0;
		curl_easy_getinfo(m_pCurl, CURLINFO_RESPONSE_CODE , &scode);
		if (scode > 0) res = (CURLcode)scode;
	}
	else {
		//error .
		fprintf(stderr, "curl_easy_perform() failed: %s\n",
			curl_easy_strerror(res));

		res = (CURLcode)-1;
	}
	curl_easy_cleanup(m_pCurl);
	return res;
}

下载文件

文件写入函数

static size_t _callback_download_response(void *contents, size_t size, size_t nmemb, void *userp)
{
	size_t realsize = size * nmemb;
	FILE* fp = (FILE *)userp;
	
	if (!fp) {
		/* out of memory! */
		printf("not file to save download data .\n");
		return 0;
	}

	if(contents && realsize > 0 ) {
		int isaved = fwrite(contents , 1 , realsize , fp);
		if(isaved != realsize){
			printf(" write file with some problems  " );
			return 0;
		}
	}
	
	return realsize;
}

下载函数

int CLibcurl::_http_download(string uri, FILE* fp) {
		
	CURL *curl = curl_easy_init(); //curl_easy_init();

	if (curl) {
		CURLcode res;
		char str_url[4096] = { 0 };
	
		sprintf_s(str_url, "%s", uri.c_str());

		curl_easy_setopt(curl, CURLOPT_POST, 0);
		curl_easy_setopt(curl, CURLOPT_URL, str_url);
		
		//跳过证书校验
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
				
		//6秒未达到30字节会退出
		curl_easy_setopt(curl,CURLOPT_LOW_SPEED_TIME,6L);
		curl_easy_setopt(curl,CURLOPT_LOW_SPEED_LIMIT,30L);

		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _callback_download_response);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
		
		res = curl_easy_perform(curl);

		if (res == CURLE_OK) {
			long scode = 0;
			curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &scode);
			if (scode > 0) res = (CURLcode)scode;
		}
		else {
			//error .
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
				curl_easy_strerror(res));
			//res = curl_easy_getinfo(curl, CURLINFO_HTTP_CODE);
			res = (CURLcode)-1;
		}
		if (hlist) curl_slist_free_all(hlist); 
		curl_easy_cleanup(curl);
		return res;
		
	}
	return -1;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

curl http或https上传下载 的相关文章

随机推荐