curl下载文件内容,读取文件大小(只能放全局去用了,如果计算百分比)

#if 1
  /*下载文件可以用write function*/
		CURL *curl_handle;
		static const char *pagefilename = "page.out";
		FILE *pagefile;

		if(argc < 2 ) {
			printf("Usage: %s <URL>\n", argv[0]);
			//return 1;
		}

		curl_global_init(CURL_GLOBAL_ALL);

		/* init the curl session */
		curl_handle = curl_easy_init();

		/* set URL to get here */
		curl_easy_setopt(curl_handle, CURLOPT_URL, "<span style="font-family: Arial, Helvetica, sans-serif;"> http://www.baidu.com/img/baidu.gif</span><span style="font-family: Arial, Helvetica, sans-serif;">");</span>

		/* Switch on full protocol/debug output while testing */
		curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

		/* disable progress meter, set to 0L to enable and disable debug output */
		curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);


		curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 30);  // 设置连接超时,单位秒
		//设置http 头部处理函数
		curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
		curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &filesize);
		// 设置文件续传的位置给libcurl
		//curl_easy_setopt(curl_handle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);


		/* send all data to this function  */
		curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

		/* open the file */
		pagefile = fopen(pagefilename, "wb");
		if (pagefile) {

			/* write the page body to this file handle */
			curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

			/* get it! */
			curl_easy_perform(curl_handle);

			/* close the header file */
			fclose(pagefile);
		}

		/* cleanup curl stuff */
		curl_easy_cleanup(curl_handle);
#endif


curl的用法库,可以处理http的各种请求



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值