c++ 使用libcurl 发送http get/post请求

        使用c++做httpclient时,可以使用libcurl库来解决,它是一个开源跨平台的网络协议库,可以去官网上下载源码编译使用,下载地址:https://curl.haxx.se/download.html,选择source archives中的一个。/docs目录有非常详细的文档,/docs/examples有很多使用的例子,有兴趣的可以看看。

         至于怎么将源码编译成静态库文件,这里就说了,网上搜一下就有。为了方便,可以直接使用我的,里面包含了libcurl的头文件、静态库libcurl和运行时需要的libcurl.dll和zlib1.dll。地址:http://download.csdn.net/detail/u014489596/9436263。

         这里简单封装了可以实现的发送http请求的 get和post方法。

libcurl.h如下:

#ifndef __LIBCURL_H__
#define __LIBCURL_H__

#include <string>
#include <iostream>
#include "curl/curl.h"


static char error_buffer[CURL_ERROR_SIZE];
static int writer(char*, size_t, size_t, std::string*);
static bool init(CURL*&, const char*, std::string*);


static bool init(CURL*& conn, const char* url, std::string* p_buffer)
{
	CURLcode code;

	//conn = curl_easy_init();
	if (NULL == conn)
	{
		std::cout << stderr <<  " Failed to create CURL connection" << std::endl;
		exit(EXIT_FAILURE);
	}

	code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, error_buffer);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set error buffer " << code << std::endl;
		return false;
	}

	code = curl_easy_setopt(conn, CURLOPT_URL, url);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set URL " << error_buffer << std::endl;
		return false;
	}

	code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set redirect option " << error_buffer << std::endl;
		return false;
	}

	code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set writer " << error_buffer << std::endl;
		return false;
	}

	code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, p_buffer);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set write data " << error_buffer << std::endl;
		return false;
	}

	return true;
}

static int writer(char* data, size_t size, size_t nmemb, std::string* writer_data)
{
	unsigned long sizes = size * nmemb;

	if (NULL == writer_data)
	{
		return 0;
	}

	writer_data->append(data, sizes);
	
	return sizes;
}



int libcurl_get(const char* url, std::string& buffer, std::string& errinfo)
{

	CURL *conn = NULL;
	CURLcode code;

	curl_global_init(CURL_GLOBAL_DEFAULT);

	if (!init(conn, url, &buffer))
	{
		std::cout << stderr << " Connection initializion failed" << std::endl;
		errinfo = "Connection initializion failed\n";

		return -1;
	}

	code = curl_easy_perform(conn);

	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to get" << url  << error_buffer << std::endl;
		
		errinfo.append("Failed to get ");
		errinfo.append(url);

		return -2;
	}

	curl_easy_cleanup(conn);
	
	return 1;
}



int libcurl_post(const char* url, const char* data, std::string& buffer, std::string& errinfo)
{
	CURL *conn = NULL;
	CURLcode code;

	curl_global_init(CURL_GLOBAL_DEFAULT);

	if (!init(conn, url, &buffer))
	{
		std::cout << stderr << " Connection initializion failed" << std::endl;
		
		errinfo = "Connection initializion failed\n";

		return -1;
	}

	code = curl_easy_setopt(conn, CURLOPT_POST, true);

	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set CURLOPT_POST " << error_buffer << std::endl;
		return -1;
	}

	code = curl_easy_setopt(conn, CURLOPT_POSTFIELDS, data);
	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to set CURLOPT_POSTFIELDS " << error_buffer << std::endl;
		return -1;
	}

	code = curl_easy_perform(conn);

	if (code != CURLE_OK)
	{
		std::cout << stderr << " Failed to post " << url << error_buffer << std::endl;

		errinfo.append("Failed to post ");
		errinfo.append(url);

		return -2;
	}

	curl_easy_cleanup(conn);
	
	return 1;
}

#endif

        使用c++做httpclient的时候只需要将此libcurl.h include进去,libcurl的头文件curl文件夹和链接需要的libcurl.lib和运行需要的libcurl.dll和zlib1.dll在上面已经已经给出了下载地址,下载直接使用。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值