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

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

		if(argc < 2 ) {
			printf("Usage: %s \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, " http://www.baidu.com/img/baidu.gif");

		/* 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的各种请求



你可能感兴趣的:(c++)