C语言典型代码---Curl实现POST

利用CURL库实现post发送

#include "include/curl/curl.h"

extern char send_basic_path[URL_LEN];
extern char send_running_path[URL_LEN];
/*************************************
初始化,在程序开始的时候调用
*************************************/
int sj_curl_init(void)
{
    curl_global_init(CURL_GLOBAL_ALL);  
    return RES_SUCCESS;
}
/*************************************
在程序退出的时候调用
*************************************/
void sj_curl_end(void)
{
    curl_global_cleanup();
    return;
}
/*************************************
回调函数,处理post返回数据的地方
*************************************/
int sj_curl_post_parse(void* buffer, size_t size, size_t nmemb, char * useless)  
{  
    char value[BUFSIZE] = {0};
    char htvalue[BUFSIZE] = {0};
    char *v = NULL;

    memcpy(value, (char *)buffer, size*nmemb);
    printf("-------%s\n", value);
    v = strstr(value, "\"data\"");
    if (NULL != v)
    {
        memcpy(htvalue, "{", 1);
        strcat(htvalue, v);
        //printf("-------%s\n", htvalue);
    }
    else
    {

    }

    return RES_SUCCESS;  
} 
/*************************************
模拟POST发送
*************************************/
int sj_curl_post_get(char *send_data, char *url)
{
    CURLcode res;
    CURL *curl;
    int iRet = RES_SUCCESS;

    curl = curl_easy_init(); 
    curl_easy_setopt(curl, CURLOPT_URL, url); //url地址  
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, send_data); //post参数  
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ft_curl_post_parse); //对返回的数据进行操作的函数地址  
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL); //这是write_data的第四个参数值  
    curl_easy_setopt(curl, CURLOPT_POST, 1); //设置问非0表示本次操作为post  
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //打印调试信息  
    //curl_easy_setopt(g_curl, CURLOPT_HEADER, 1); //将响应头信息和相应体一起传给write_data  
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置为非0,响应头信息location  
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");  
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);


    res = curl_easy_perform(curl);   
    if (res != CURLE_OK)
    {  
        switch(res)  
        {
            case CURLE_UNSUPPORTED_PROTOCOL:  
                smart_log(ERROR,"Does not support agreement","FAIL");
            break;
            case CURLE_COULDNT_CONNECT:  
                smart_log(ERROR,"not link remote server","FAIL");
            break;
            case CURLE_HTTP_RETURNED_ERROR:  
                smart_log(ERROR,"http return failed","FAIL");
            break;
            case CURLE_READ_ERROR:  
                smart_log(ERROR,"read local file failed","FAIL");
            break;
                default:  
                    smart_log(ERROR,"curl other error","FAIL");
                break;
        }

        iRet = RES_FAIL;

    }
    else
    {
        iRet = RES_SUCCESS;
    }
        curl_easy_cleanup(curl);
    return iRet;
}

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,如果你需要在C语言中使用curl库发送multipart/form-data格式的POST请求,可以参考以下步骤: 1. 创建一个curl实例,并设置请求地址和请求方法为POST。 2. 设置请求头信息,包括Content-Type和boundary。 3. 组装请求参数,将需要上传的文件转化为二进制数据,并按照multipart/form-data格式进行拼接。 4. 使用curl_easy_setopt函数设置CURLOPT_POSTFIELDS选项,将请求参数作为POST请求的数据发送。 5. 调用curl_easy_perform函数发送请求并获取响应。 下面是一个示例代码,假设需要上传一个名为file.txt的文件: ```c #include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: multipart/form-data; boundary=------------------------1234567890"); curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/upload"); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); const char *boundary = "------------------------1234567890"; const char *filename = "/path/to/file.txt"; FILE *fp = fopen(filename, "rb"); fseek(fp, 0, SEEK_END); long file_len = ftell(fp); fseek(fp, 0, SEEK_SET); char *file_data = malloc(file_len); fread(file_data, file_len, 1, fp); fclose(fp); char *request_data; asprintf(&request_data, "--%s\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "Content-Type: application/octet-stream\r\n" "\r\n" "%s\r\n" "--%s--\r\n", boundary, filename, file_data, boundary); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data); res = curl_easy_perform(curl); curl_slist_free_all(headers); curl_easy_cleanup(curl); free(request_data); if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } } curl_global_cleanup(); return 0; } ``` 在示例代码中,我们创建了一个名为curlcurl实例,并设置请求地址和请求方法为POST。然后,我们设置了请求头信息,包括Content-Type和boundary。接着,我们读取文件内容并组装请求参数,最后使用curl_easy_setopt函数设置CURLOPT_POSTFIELDS选项,将请求参数作为POST请求的数据发送。最后,我们调用curl_easy_perform函数发送请求并获取响应。 你可以根据自己的需求进行更改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖哥王老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值