之前介绍了使用libcurl的HTTP GET将url地址中内容下载到本地
C/C++中libcurl的使用-Http GET方法使用详解
,在更早的文章
Linux下使用CURL模拟用户提交post表单
中也介绍过在Linux环境使用curl命令提交POST表单。本文介绍使用libcurl的HTTP POST实现表单的提交,并获取表单的结果。
libcurl的相关接口已经在上一篇文章中介绍,本文只介绍新增的相关接口和参数选项。
CURL_EXTERN struct curl_slist *curl_slist_append(struct curl_slist *,const char *);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POST, long post);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTFIELDS, char *postdata);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_POSTFIELDSIZE, long size);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_COOKIEFILE, char *filename);
//如果提交POST请求要读取cookie信息,可以使用如下的参数将cookie所在的地址传递进来,cookie数据的格式可以是旧版本的"Netscape / Mozilla"cookie格式,也可以是普通的HTTP header的dump文件。
示例代码:
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct MemoryStruct
char *memory;
size_t size;
MemoryStruct()
memory = (char *)malloc(1);
size = 0;
~MemoryStruct()
free(memory);
memory = NULL;
size = 0;
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory)
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
int main()
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(CURLE_OK != res)
cout<<"curl init failed"<<endl;
return 1;
CURL *pCurl ;
pCurl = curl_easy_init();
if( NULL == pCurl)
cout<<"Init CURL failed..."<<endl;
return -1;
string url = "http://{IP}:{PORT}/search";
string filename = "result.json";
curl_slist *pList = NULL;
pList = curl_slist_append(pList,"Content-Type:application/x-www-form-urlencoded; charset=UTF-8");
pList = curl_slist_append(pList,"Accept:application/json, text/javascript, */*; q=0.01");
pList = curl_slist_append(pList,"Accept-Language:zh-CN,zh;q=0.8");
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList);
curl_easy_setopt(pCurl, CURLOPT_URL, url.c_str() );
curl_easy_setopt(pCurl, CURLOPT_HEADER, 0L);
curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1L);
MemoryStruct oDataChunk;
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &oDataChunk);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L);
string strJsonData;
strJsonData = "queryWord=CONTENT:码农&";
strJsonData += "startTime=1507605327&" ;
strJsonData += "endTime=1508210127&" ;
strJsonData += "maxCount=500&" ;
strJsonData += "contextLength=200" ;
curl_easy_setopt(pCurl, CURLOPT_POST, 1L);
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, strJsonData.c_str());
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, strJsonData.size());
res = curl_easy_perform(pCurl);
long res_code=0;
res=curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &res_code);
if(( res == CURLE_OK ) && (res_code == 200 || res_code == 201))
FILE* fTmpMem = (FILE*)oDataChunk.memory;
if (!fTmpMem) {
FILE *fp=fopen(filename.c_str(),"wb");
if(!fp)
cout<<"open file failed";
return -1;
fwrite(fTmpMem, 1, oDataChunk.size, fp);
fclose(fp);
return true;
curl_slist_free_all(pList);
curl_easy_cleanup(pCurl);
curl_global_cleanup();
return 0;
在上述代码中,使用libcurl的POST选项将表单数据提交到相应的服务器,在正确响应结果后,将返回的json格式的数据写到本地的磁盘文件上。
之前介绍了使用libcurl的HTTP GET将url地址中内容下载到本地 C/C++中libcurl的使用-Http GET方法使用详解,在更早的文章Linux下使用CURL模拟用户提交post表单中也介绍过在Linux环境使用curl命令提交POST表单。本文介绍使用libcurl的HTTP POST实现表单的提交,并获取表单的结果。libcurl的相关接口已经在上一篇文章中介绍,本文只介
std::string url = "http://xxxx";
std::string postParams = "aa=33&bb=33";
std::string response;
curl_post_req("http://sss", "aa=33&bb=33", response);
// 程序结束前清理curl
libcurl的easy interface是最基本的用法,简要流程为:
1、在主线程中调用curl_global_init(CURL_GLOBAL_ALL)初始化libcurl
2、调用curl_easy_init获取一个句柄,得到 easy interface型指针;
3、调用curl_easy_setopt函数设置此次传输的一些基本参数,如url地...
libcurl 是一个使用比较广泛,并且使用很方便的http请求库。
对于http post请求,根据head 部分的Content-Type 的不同,处理方式也不同。1. 普通post请求 一般的post请求格式:
Content-Type: application/x-www-form-urlencoded
对应请求数据包:POST /test.php HTTP/1.1
Accept: */
C++如何使用libcurl发送post请求的三种content-type的封装
C++如何使用libcurl发送post请求的3中content-type的封装。
我们知道http协议的post请求的content-type有三种方式,其他语言都能很方便的设置,那么c++如何实现post请求不同content-type的请求能,下面封装好了大家需要可以直接拿
里面用到的json库可以参照这篇文章C++
CURLcode res;
FILE *file;
char *url = "http://example.com/upload_image.php"; // 上传图片的URL地址
char *filename = "example.jpg"; // 需要上传的图片文件名
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// 设置上传文件的参数
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "image",
CURLFORM_FILE, filename,
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
// 清理上传文件的参数
curl_formfree(formpost);
curl_easy_cleanup(curl);
return 0;
以上代码使用了libcurl库,可以使用`curl_easy_setopt`函数设置HTTP请求的参数,使用`curl_formadd`函数设置上传文件的参数。具体来说,使用`CURLFORM_COPYNAME`指定上传文件的参数名,使用`CURLFORM_FILE`指定需要上传的文件名。最后使用`curl_easy_perform`函数执行HTTP请求,并使用`curl_formfree`函数清理上传文件的参数。
需要注意的是,以上代码仅供参考,具体实现需要根据具体的情况进行修改。特别是需要根据实际情况设置上传图片的URL地址和需要上传的图片文件名。