C++用libcurl通过HTTP以表单的方式Post数据到服务器

Posted liuzhenbo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++用libcurl通过HTTP以表单的方式Post数据到服务器相关的知识,希望对你有一定的参考价值。

POST字符串

#include <stdio.h>
#include <curl/curl.h>
 
int main(void)

    CURL* curl = NULL;
    CURLcode res;
 
    curl = curl_easy_init();
    if(curl == NULL) 
    
        return CURLE_FAILED_INIT;
    
 
    struct curl_slist* headerlist = NULL; 
 
    // 设置表头,表头内容可能不同
    headerlist = curl_slist_append(headerlist, "Content-Type:application/x-www-form-urlencoded");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
 
    // 设置URL
    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
 
    // 设置参数,比如"ParamName1=ParamName1Content&ParamName2=ParamName2Content&..."
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
 
    // 设置为Post
    curl_easy_setopt(curl, CURLOPT_POST, 1);
 
    // 发送
    res = curl_easy_perform(curl);
    
    if(res != CURLE_OK)
    
        // 获取详细错误信息
        char* szErr = curl_easy_strerror(res);
        fprintf(stderr, "curl_easy_perform() failed: %s\n", szErr);
    
 
    // 清空
    curl_easy_cleanup(curl);
 
    // 释放表头
    curl_slist_free_all (headerlist);  
    
    return 0;

POST文件

#include <stdio.h>
#include <curl/curl.h>
 
size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid);
 
int main(void)

    CURL* curl = NULL;
    CURLcode res;
 
    curl = curl_easy_init();
    if(curl == NULL) 
    
        return CURLE_FAILED_INIT;
    
 
    struct curl_slist*        headerlist    = NULL; 
    struct curl_httppost*    formpost    = NULL;
    struct curl_httppost*    lastptr        = NULL;
    std::string strResponse;    // 回复
 
    curl_formadd(&formpost, &lastptr, 
        CURLFORM_COPYNAME, "ParamName1", 
        CURLFORM_COPYCONTENTS, "ParamName1Content", CURLFORM_END);
 
    curl_formadd(&formpost, &lastptr, 
        CURLFORM_COPYNAME, "ParamName2", 
        CURLFORM_COPYCONTENTS, "ParamName2Content", CURLFORM_END);
 
    curl_formadd(&formpost, &lastptr, 
        CURLFORM_COPYNAME, "ParamName3", 
        CURLFORM_COPYCONTENTS, "ParamName3Content", CURLFORM_END);
 
    curl_formadd(&formpost, &lastptr, 
        CURLFORM_COPYNAME, "registerImgs", 
        CURLFORM_FILE, "C:/Image.png", CURLFORM_END);    // 设置要上传的文件
 
    // 设置表单参数
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
 
    // 设置表头,表头内容可能不同
    headerlist = curl_slist_append(headerlist, "Content-Type:application/x-www-form-urlencoded");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
 
    // 设置URL
    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
 
    // 设置参数,比如"ParamName1=ParamName1Content&ParamName2=ParamName2Content&..."
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
 
    // 设置为Post
    curl_easy_setopt(curl, CURLOPT_POST, 1);
 
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
 
    // 发送
    res = curl_easy_perform(curl);
    
    if(res != CURLE_OK)
    
        // 获取详细错误信息
        char* szErr = curl_easy_strerror(res);
        fprintf(stderr, "curl_easy_perform() failed: %s\n", szErr);
    
 
    // 清空
    curl_easy_cleanup(curl);
 
    // 释放表单
    curl_formfree(formpost);
 
    // 释放表头
    curl_slist_free_all (headerlist);  
    
    return 0;

 
size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)

    std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
    if( NULL == str || NULL == buffer )
    
        return -1;
    
 
    char* pData = (char*)buffer;
    str->append(pData, size * nmemb);
    return nmemb;

 

以上是关于C++用libcurl通过HTTP以表单的方式Post数据到服务器的主要内容,如果未能解决你的问题,请参考以下文章

C++ 用libcurl库进行http通讯网络编程 转

转:Curl详解

curl详解

Linux之curl命令详解

用 C++ 构建 libcurl 库,菜鸟问题

如何使用 libcurl 进行 HTTP 发布?