C 语言使用Libcurl /curl 发送数据 (可以设置http header)

1.环境Windows 

2.依赖库文件

头文件:

<curl.h>

lib:

  • libcurl.a
  • libcurl.dll.a

dll:

libcurl.dll

下载地址:https://curl.haxx.se/windows/

3.程序


//#define CURL_STATICLIB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <frozen-master/frozen.h>

#define myurl   "www.helloworld.com"



struct string {
	char *ptr;
	size_t len;
};

void init_string(struct string *s) {
	s->len = 0;
	s->ptr = malloc(s->len + 1);
	if (s->ptr == NULL) {
		fprintf(stderr, "malloc() failed\n");
		exit(EXIT_FAILURE);
	}
	s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
	size_t new_len = s->len + size * nmemb;
	s->ptr = realloc(s->ptr, new_len + 1);
	if (s->ptr == NULL) {
		fprintf(stderr, "realloc() failed\n");
		exit(EXIT_FAILURE);
	}
	memcpy(s->ptr + s->len, ptr, size*nmemb);
	s->ptr[new_len] = '\0';
	s->len = new_len;

	return size * nmemb;
}

int main(void)
{
	CURL *curl;
	CURLcode res;
	char *mytopic = NULL;
	char *mybootstrap_servers = NULL;



	curl = curl_easy_init();
	if (curl) {
		struct string s;
		init_string(&s);
		
		struct curl_slist *chunk = NULL;

		/* Remove a header curl would otherwise add by itself */
		chunk = curl_slist_append(chunk, "Agent-Role:Your_Role");

		/* Add a custom header */
		chunk = curl_slist_append(chunk, "Agent-User:Username");

		/* Modify a header curl otherwise adds differently */
		chunk = curl_slist_append(chunk, "Agent-Password:Password");

		/* Add a header with "blank" contents to the right of the colon. Note that
		   we're then using a semicolon in the string we pass to curl! */
		//chunk = curl_slist_append(chunk, "X-silly-header;");

		/* set our custom set of headers */
		 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);


		//curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
		curl_easy_setopt(curl, CURLOPT_URL, myurl);
		/* example.com is redirected, so we tell libcurl to follow redirection */
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);


		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

		/* 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));

	   printf("%s\n", s.ptr);

	   free(s.ptr);

		/* always cleanup */
		curl_easy_cleanup(curl);
	}

	return 0;
}

另附官方的简单curl使用例程(具体参考官网:https://curl.haxx.se):

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

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

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* 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));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

 

 

参考:

1.https://curl.haxx.se/libcurl/c/http-post.html

2.https://curl.haxx.se/libcurl/c/httpcustomheader.html

3.https://curl.haxx.se/libcurl/c/example.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值