libcurl实现POST GET

1、POST

int curl_post(void)
{
	CURL *curl;
	CURLcode res;

	printf("[%s]start\n",__func__);

	/* In windows, this will init the winsock stuff */ 
	curl_global_init(CURL_GLOBAL_ALL);

	/* get a curl handle */ 
	curl = curl_easy_init();
	if(curl) {
		// 设置POST URL
		curl_easy_setopt(curl, CURLOPT_URL, "192.168.16.111");
		curl_easy_setopt(curl, CURLOPT_POST, 1L);

		// 自定义head
		struct curl_slist *head = NULL;
		head = curl_slist_append(head, "Content-Type: application/json");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head);

		// POST 内容
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "hello");

		// 设置超时时间20s
		curl_easy_setopt(curl, CURLOPT_TIMEOUT,20);

		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		/* Check for errors */ 
		if(res != CURLE_OK){
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
					curl_easy_strerror(res));
		}
		else{
			int retcode=0;
			curl_easy_getinfo(curl,CURLINFO_HTTP_CODE,&retcode);
			printf("[%s]retcode=%d\n",__func__,retcode);
		}
		/* always cleanup */ 
		curl_easy_cleanup(curl);
		curl_slist_free_all(head);
	}

	curl_global_cleanup();

	return 0;
}

2、GET


// http://203.107.1.33/100000/d?host=www.aliyun.com
#define ACC_ID "195221"
#define GETURL_FORMAT "http://203.107.1.33/"ACC_ID"/d?host=%s"

static size_t
write_cb(void *contents, size_t size, size_t nmemb, void *userp)
{
	size_t realsize = size * nmemb;
	static int len=0;

	char *ptr = realloc((char*)userp, len + realsize + 1);
		if(!ptr) {
		/* out of memory! */ 
		printf("not enough memory (realloc returned NULL)\n");
		return 0;
	}

	userp = ptr;

	memcpy(ptr+len, contents, realsize);
	len += realsize;

	printf("[%s]get len=%d\n",__func__,realsize);

	return realsize;
}

static int total_len=0;
static size_t
receive_cb(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;

  memcpy(userp+total_len, contents, realsize);
  total_len += realsize;

  printf("[%s]get len=%d\n",__func__,realsize);

  return realsize;
}

int curl_get(void)
{
	CURL *curl;
	CURLcode res;
	
	printf("[%s]start\n",__func__);
	
	/* In windows, this will init the winsock stuff */ 
	curl_global_init(CURL_GLOBAL_ALL);
	
	/* get a curl handle */ 
	curl = curl_easy_init();
  	if(curl) {
		char get_url[256]={0};
		char *get_buf=malloc(1024);
		char *head_buf=malloc(1024);

		sprintf(get_url,GETURL_FORMAT,"www.aliyun.com");
		printf("[%s]get_url:%s\n",__func__,get_url);
		// 设置GET URL
		curl_easy_setopt(curl, CURLOPT_URL, get_url);

		// 设置支持http1.0
		curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

		// 读取数据
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, get_buf);

		memset(head_buf,0,1024);
		// 读取head
		curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, receive_cb);
		curl_easy_setopt(curl, CURLOPT_HEADERDATA, head_buf);
		
		// 支持设置重定向
		//curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
		
		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		/* Check for errors */ 
		if(res != CURLE_OK){
		fprintf(stderr, "curl_easy_perform() failed: %s\n",
				curl_easy_strerror(res));
		}else{
			int retcode=0;
			curl_easy_getinfo(curl,CURLINFO_HTTP_CODE,&retcode);
			printf("[%s]retcode=%d\n",__func__,retcode);
			
			printf("[%s]head:\n%s\n",__func__,head_buf);
			printf("[%s]get:\n%s\n",__func__,get_buf);
			
		}
		

		/* always cleanup */ 
		curl_easy_cleanup(curl);

		if(get_buf)
			free(get_buf);
		if(head_buf)
			free(head_buf);
  	}
  
	curl_global_cleanup();
	return 0;
}

3、download file

// 下载文件

static size_t
download_cb(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t written = fwrite(contents, size, nmemb, userp);

  printf("[%s]written=%d\n",__func__,written);
  
  return written;
}


#define DL_URL  "http://192.168.16.111"
#define DL_PATH "./test.bin"

#define DLURL_FORMAT DL_URL"/http/test.bin"
int curl_down(void)
{
	CURL *curl;
	CURLcode res;

	printf("[%s]start\n",__func__);
	

	/* In windows, this will init the winsock stuff */ 
	curl_global_init(CURL_GLOBAL_ALL);

	/* get a curl handle */ 
	curl = curl_easy_init();
	if(curl) {
		FILE  *fp = fopen(DL_PATH, "w+"); //打开文件 
		if (!fp) {
			printf("fopen failed\n");
			goto err;
		}
		
		// 设置 GET url
		curl_easy_setopt(curl, CURLOPT_URL, DLURL_FORMAT);

		// 支持http 1.1
		// curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

		// 设置超时时间20s
		curl_easy_setopt(curl, CURLOPT_TIMEOUT,20);

		// 写入数据
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_cb);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		/* Check for errors */ 
		if(res != CURLE_OK){
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
				curl_easy_strerror(res));
		}else{
			fseek(fp, 0L, SEEK_END);
			unsigned long filesize = ftell(fp);

			printf("[%s]download done,size:%d\n",__func__,filesize);
		}
		
		printf("[%s]down done\n",__func__);

		fclose(fp);
err:
		/* always cleanup */ 
		curl_easy_cleanup(curl);

	}

	curl_global_cleanup();

	return 0;
}

仅此记录

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值