Libcurl安装与HelloWorld

Windows系统下源代码下载编译、安装方式如下:
https://blog.csdn.net/fxbjye/article/details/89152849
编译后得到库文件,把这两个文件复制到项目文件中,
在这里插入图片描述
修改项目文件的属性,
在这里插入图片描述
修改附加依赖项:
在这里插入图片描述
输入代码:

#include <stdio.h> 
#include <string.h> 
#include <curl.h> 

#define MAX_BUF 	 65536 

char wr_buf[MAX_BUF + 1];
int  wr_index;

/*
* Write data callback function (called within the context of
* curl_easy_perform.
*/
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
	int segsize = size * nmemb;

	/* Check to see if this data exceeds the size of our buffer. If so,
	* set the user-defined context value and return 0 to indicate a
	* problem to curl.
	*/
	if (wr_index + segsize > MAX_BUF) {
		*(int *)userp = 1;
		return 0;
	}

	/* Copy the data from the curl buffer into our buffer */
	memcpy((void *)&wr_buf[wr_index], buffer, (size_t)segsize);

	/* Update the write index */
	wr_index += segsize;

	/* Null terminate the buffer */
	wr_buf[wr_index] = 0;

	/* Return the number of bytes received, indicating to curl that all is okay */
	return segsize;
}

/*
* Simple curl application to read the index.html file from a Web site.
*/
int main(void)
{
	CURL *curl;
	CURLcode ret;
	int  wr_error;

	wr_error = 0;
	wr_index = 0;

	/* First step, init curl */
	curl = curl_easy_init();
	if (!curl) {
		printf("couldn't init curl\n");
		return 0;
	}

	/* Tell curl the URL of the file we're going to retrieve */
	curl_easy_setopt(curl, CURLOPT_URL, "www.baidu.com");

	/* Tell curl that we'll receive data to the function write_data, and
	* also provide it with a context pointer for our error return.
	*/
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&wr_error);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

	/* Allow curl to perform the action */
	ret = curl_easy_perform(curl);

	printf("ret = %d (write_error = %d)\n", ret, wr_error);

	/* Emit the page if curl indicates that no errors occurred */
	if (ret == 0) printf("%s\n", wr_buf);

	curl_easy_cleanup(curl);

	return 0;
}

输出结果为:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值