Sword libcurl使用

libcurl编译
>> ./configure --prefix=/opt/soft/curl --disable-ldap --disable-ldaps
>> make
>> make install
注意点:如果使用libcurl.a静态库,那么在链接是需要增加-lrt -lcrypto -lssl -lpthread -lz这几个动态库
#include <iostream>
#include <stdlib.h>
#include <string.h>

#include "curl/curl.h"

using namespace std;

//该结构体完全可以自定义,只需要在回调函数中注意使用
struct MemoryStruct {
    char *memory;
    size_t size;
};

//请求结果后的回调函数
static size_t callback(void *contents, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    //内存分配可以自由控制
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;

    mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
    if (mem->memory == NULL) {
        /* out of memory! */
        printf("not enough memory (realloc returned NULL)\n");
        return 0;
    }

    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;

    return realsize;
}

//get方式访问
int url_get()
{
    //easy handler句柄
    CURL *curl_handle;

    //easy 错误码
    CURLcode res;
    struct MemoryStruct chunk;

    //HTTP报文头
    struct curl_slist* headers = NULL;

    /*
    这里是否分配内存,完全取决于CURLOPT_WRITEFUNCTION选项中的回调函数需要怎样分配内存,可以自由发挥
    */
    chunk.memory = (char *)malloc(1);  
    chunk.size = 0;   

     /*
        这个函数只能用一次,如果这个函数在curl_easy_init函数调用时还没调用,
        它讲由libcurl库自动调用,所以多线程下最好在主线程中调用一次该函数以防止在线程
        中curl_easy_init时多次调用
    */
    res = curl_global_init(CURL_GLOBAL_ALL);
    if (CURLE_OK != res)
    {
        //初始化失败
        return -1;
    }

    //初始化easy handler句柄
    curl_handle = curl_easy_init();
    if (NULL == curl_handle)
    {
        //easy handler句柄初始化失败
        return -1;
    }

    //构建HTTP报文头--可以填写多个报文头
    headers = curl_slist_append(headers,"Accept-Charset:utf-8");
    headers = curl_slist_append(headers,"Accept-Language: en-US");
    //设置HTTP报文头
    curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);

    //请求超时时长
    curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 3L);

    //连接超时时长 
    curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 10L); 

    //允许重定向
     curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);

     //设置为非0将响应头信息同响应体一起传给WRITEFUNCTION
     curl_easy_setopt(curl_handle, CURLOPT_HEADER, 0L);

      //得到请求结果后的回调函数
     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, callback); 

     //获取请求结果
     //特别注意:CURLOPT_WRITEDATA必须和CURLOPT_WRITEFUNCTION一起使用,
     //因为chunk就是CURLOPT_WRITEFUNCTION的第四个参数
     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &chunk);

     //设置需要访问的url地址
     //注意:必选项,不可缺少
     curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.baidu.com/?tn=98010089_dg"); 

     //执行请求
     res = curl_easy_perform(curl_handle);

     //获取HTTP返回码
     long res_code = 0;
     res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &res_code);

     if (!(res == CURLE_OK && (200 == res_code || 201 == res_code)))
     {
         //执行请求失败--也可以不判断HTTP返回码
         return -1;
     }

     //将下载下来的数据存在本地
     FILE *pfw = fopen("1.html", "w");
     fwrite(chunk.memory, chunk.size, 1, pfw);
     fclose(pfw);

     //清理HTTP报文头
     curl_slist_free_all(headers);

     //清理easy handler句柄
     curl_easy_cleanup(curl_handle);

     //清理全局变量
     curl_global_cleanup();

    return 0;
}

//post方式访问
int url_post()
{
    CURL *curl_handle;

    CURLcode res;
    struct MemoryStruct chunk;

    chunk.memory = (char *)malloc(1);
    chunk.size = 0;

    res = curl_global_init(CURL_GLOBAL_ALL);
    if (CURLE_OK != res)
    {
        return -1;
    }
    curl_handle = curl_easy_init();
    if (NULL == curl_handle)
    {
        return -1;
    }

    //post参数之间使用 & 分隔
    char *post_arg = (char *)"user_id=speech&domain=1&language=zh&speed=5&text=标贝语音,为您服务";

    //设置post参数
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_arg);

    // 设置为非0表示本次操作为POST
    curl_easy_setopt(curl_handle, CURLOPT_POST, 1);

    //得到请求结果后的回调函数
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, callback);

    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &chunk);

    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://xxx/tts");

    //执行请求
    res = curl_easy_perform(curl_handle);

    //获取HTTP返回码
    long res_code = 0;
    res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &res_code);

    if (!(res == CURLE_OK && (200 == res_code || 201 == res_code)))
    {
        //执行请求失败--也可以不判断HTTP返回码
        return -1;
    }

    FILE *pfw = fopen("1.mp3", "w");
    fwrite(chunk.memory, chunk.size, 1, pfw);
    fclose(pfw);

    curl_easy_cleanup(curl_handle);
    curl_global_cleanup();

    return 0;

}

int main()
{
    url_post();
    return 0;
}
//当url中有特殊字符时,例如空格等,需要使用curl_easy_escape转码
CURL *curl = curl_easy_init();
if(curl) {
  char *output = curl_easy_escape(curl, "data to convert", 15);
  if(output) {
    printf("Encoded: %s\n", output);
    curl_free(output);
  }
}
//注意需要调用curl_free()函数释放返回值
liburl动态需要依赖ssl库文件libcrypto.so libssl.so

 

posted on 2018-09-27 22:16  寒魔影  阅读(378)  评论(0编辑  收藏  举报

导航