您的位置:首页 > 理论基础 > 计算机网络

libcurl安装和简单实例

2017-01-12 20:34 190 查看
libcurl 官网:https://curl.haxx.se/

libcurl也是curl工具使用的库

可以到官网下载,更方便的是到github上克隆代码

git clone https://github.com/curl/curl.git
编译和安装

cd curl

./buildconf

./configure

make

sudo make install

安装完毕之后,头文件

/usr/local/include/curl

/usr/local/lib/libcurl.so

可执行命令

/usr/local/bin/curl

> 另外一种办法是:可以简单的执行

sudo apt-get install curl

安装curl命令,该命令将curl安装/usr/bin

sudo apt-get install libcurl4-openssl-dev

来安装libcurl,libcurl的安装路径

头文件:/usr/include/curl

库目录:/usr/lib

> libcurl 与 tufao 通信: 发送请求与响应请求

#include <curl/curl.h>

#include "../h.h"

// ptr: data from server

// size: size of block

// nmemb: block counts

//      totalsize = size * nmemb

// userdata: user input,   CURLOPT_WRITEDATA

// return: output datalen

size_t wirte_callback(char* ptr, size_t size, size_t nmemb, void* userdata)

{

    char* buf = (char *)malloc(size * nmemb + 1);

    memcpy(buf, ptr, size*nmemb);

    buf[size*nmemb] = 0;

    printf("recv data = %s\n", buf);

    printf("*********************\n");

    free(buf);

    return size * nmemb;

}

int main(int argc, char* argv[])

{

    // init curl

    CURLcode code = curl_global_init(CURL_GLOBAL_ALL);

    if (code != CURLE_OK)

    {

        printf("global init error\n");

        return 1;

    }

    // create CURL object

    CURL* curl = curl_easy_init();

    // set URL, send address;

    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.49.136:8080");

    // set callback function, get response data

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wirte_callback);

    // set callback function userdata

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);

    // send http request

    code = curl_easy_perform(curl);

    if (code != CURLE_OK)

    {

        printf("perform error\n");

        return 2;

    }

    // 释放 curl

    curl_easy_cleanup(curl);

    return 0;

}

> libcurl发送post请求

#include "../h.h"

#include <curl/curl.h>

size_t wirte_callback(char* ptr, size_t size, size_t nmemb, void* userdata)

{

    char* buf = (char *)malloc(size * nmemb + 1);

    memcpy(buf, ptr, size*nmemb);

    buf[size*nmemb] = 0;

    printf("recv data = %s\n", buf);

    printf("*********************\n");

    free(buf);

    return size * nmemb;

}

int main()

{

    CURLcode code = curl_global_init(CURL_GLOBAL_ALL);

    if (code != CURLE_OK)

    {

        printf("global init err\n");

        return -1;

    }

    CURL* curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.49.136:8081");

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wirte_callback);

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);

    // set post method

    curl_easy_setopt(curl, CURLOPT_POST, 1);

    // set post data

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "this is post data");

    // set opst size, if is text, can not option.

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, sizeof("this is post data"));

    code = curl_easy_perform(curl);

    if (code != CURLE_OK)

    {

        printf("perform err\n");

        return -2;

    }

    curl_easy_cleanup(curl);

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息