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;
}

 

posted @ 2019-06-19 19:14  心田居士  阅读(6946)  评论(0编辑  收藏  举报