记录一下以前写的基于CURL的HTTP请求代码,方便以后查找和使用。
HTTP GET请求:
int get_test()
{
char *url = "www.baidu.com";
char *result = (char *) calloc(1, sizeof(char));
size_t size = 0;
int ret = http_get_request(url, &result, &size);
if (ret < 0 || result == NULL)
{
printf("Request error,error code %dn", ret);
return -1;
}
else
{
printf("%s", result);
}
free(result);
return 0;
}
HTTP POST请求
int post_test()
{
char *url = "www.baidu.com";
char *data = "?data=fgdsgh";
char *result = (char *) calloc(1, sizeof(char));
size_t size = 0;
int ret = http_post_request(url,data, &result, &size);
if (ret < 0 || result == NULL)
{
printf("Request error,error code %dn", ret);
return -1;
}
else
{
printf("%s", result);
}
free(result);
return 0;
}
封装:
文章评论