libcurl 简单使用 - yanbin's Blog

libcurl 简单使用

yanbin posted @ 2015年3月15日 17:26 in Networking , 3468 阅读

1. 必须首先调用 curl_global_init(), 并且指定 CURL_GLOBAL_ALL 作为参数。

    curl_global_init() 会分配一些内存,用于内部的实现。

 

2. 调用 curl_global_cleanup() 清除 curl_global_init() 分配的资源。
    当然这个函数可以在程序任何执行之中调用,不一定是程序将要结束之前调用。
    在完成了所有与 liburl 有关的工作之后,调用 curl_global_cleanup().
 
3. 使用 easy-functions  或者 multi-functions
    1) easy-functions 一般来说可以满足基本工作。
    2) 调用 CURL *curl_easy_init() 获得一个 easy session handle, 其实可以说是获得一个 curl handle.
    3) 使用 curl_easy_setopt(); 指定一个要请求的 URL.
    4) 使用 curl_easy_perform(); 完成请求,这里可能会阻塞。
    5) 使用 curl_easy_cleanup(curl), 完成清理工作。
 
4) curl_global_init() 一般会从系统分配内存。
    也可以使用 curl_global_init_mem() 指定一个自定义的内存地址给 libcurl  用来分配内存 ,这个地址可以是 stack, 也可以是动态分配的。
 
5) 小例子。
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#define REMOTE_URL   "http://www.google.com"
int main(int argc, char **argv)
{
     CURL *curl; 
     CURLcode res;
     curl_global_init(CURL_GLOBAL_ALL);
     curl = curl_easy_init();
     if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) 
           fprintf(stderr, "curl_easy_perform error: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return 0;
}

 

Avatar_small
Emma 说:
2022年12月08日 22:20

libcurl is a simple and easy to use software library that enables developers to quickly and easily integrate HTTP and HTTPS transfer diamonds rings for cheap capabilities into their applications. Written in C, libcurl can be used in conjunction with a wide variety of programming languages and platforms, making it an extremely versatile and powerful tool.

Avatar_small
charlly 说:
2023年1月07日 12:58

libcurl is an easy to use library that allows developers to quickly add support for basic network functions in their applications. It is portable,plays well with a wide variety of operating systems and compilers, and supports a wide range of protocols. This makes it a popular choice for developers looking to nashvilles tax related law add network functionality to their applications with minimal effort.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee