使用c++做httpclient时,可以使用libcurl库来解决,它是一个开源跨平台的网络协议库,可以去官网上下载源码编译使用,下载地址:https://curl.haxx.se/download.html,选择source archives中的一个。/docs目录有非常详细的文档,/docs/examples有很多使用的例子,有兴趣的可以看看。
至于怎么将源码编译成静态库文件,这里就说了,网上搜一下就有。为了方便,可以直接使用我的,里面包含了libcurl的头文件、静态库libcurl和运行时需要的libcurl.dll和zlib1.dll。地址:http://download.youkuaiyun.com/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在上面已经已经给出了下载地址,下载直接使用。