LIBCURL踩坑记

这里一个java程序员进行C++开发使用libcurl踩过的坑:

1.  发送指定请求类型body,比较通用方法如下,可以直接填写数据并手动指定content type,如果是form表单等形式,则需要参考curl_formadd方法

    struct curl_slist *list = NULL;
    list = curl_slist_append(list, "Content-Type: application/octet-stream");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

    /* size of the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, bufferLen);
    /* pass in a pointer to the data - libcurl will not copy 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer);

2. 响应结果包含了响应行和头域信息

curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);

如上所示,得到的response包含响应行和头域信息,而且网上也没有相关问题描述。原因为设置了如下参数。

        //CURLOPT_HEADER设置为1时,回调数据会返回响应行和header行
        curl_easy_setopt(curl, CURLOPT_HEADER, 1);

猜你喜欢

转载自www.cnblogs.com/lmsthoughts/p/9229608.html