1:CURLcode curl_global_init(long flags);

这个函数全局需要调用一次(多次调用也可以,不过没有必要), 所以这也是把Curlplus设计成单体类的原因,curl_global_init函数在其他libcurl函数调用前至少调用一次,程序最后需要调用curl_global_cleanup,进行清理。

参数:flags

CURL_GLOBAL_ALL Initialize everything possible. This sets all known bits.

CURL_GLOBAL_SSL Initialize SSL

CURL_GLOBAL_WIN32 Initialize the Win32 socket libraries.

CURL_GLOBAL_NOTHING Initialise nothing extra. This sets no bit.

CURLcode 是一个enum,当CURLcode为CURLE_OK时,表示函数执行成功,否则失败,具体错误原因可以查看<curl/curl.h>文件内的定义。

2:curl_easy_init() - Start a libcurl easy session

curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理. 一般curl_easy_init意味着一个会话的开始. 它的返回值是CURL *,curl_easy_init函数是线程相关的,也就是说不能在一个线程中调用另外一个线程通过curl_easy_init创建的CURL指针。

3:CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);

描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.,要想具体了解CURL的行为,必须对CURLoption有足够的了解,具体可以参考:

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

这里有两个类型不易理解CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Curlplus::writer);

设置一个回调函数,这个回调函数的格式是

size_t function( void *ptr, size_t size, size_t nmemb, void *stream);

ptr,返回数据的指针

size,返回数据每块的大小

nmemb,返回数据的块数(这里返回数据串的真正大小为size*nmemb)

stream,是curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); 中的buffer的指针。

在上面的例子中,buffer设置为一个string对象,所以,在回调函数writer中有了writerData->append(data, len);

4:CURLcode curl_easy_perform(CURL *handle);

执行远程请求

参考资料

http://curl.haxx.se/

http://curl.haxx.se/lxr/source/docs/examples/

基于curl 的C API写了一个扩展C++ singleton类(当然curl也有C++ API),这个单体类只是对HTTP请求做了简单封装,提供post,get方法,并得到请求url内的返回值(保存到string对象中),也很容易扩展到其他协议上去。

Curlplus.h文件

#ifndef _CURLPLUS_H__

#define _CURLPLUS_H__

#ifndef __CURL_CURL_H

#include <curl/curl.h>

#endif

#ifndef __CURL_EASY_H

#include <curl/easy.h>

#endif

#include <memory>

#include <string>

using namespace::std;

namespace CP

{

class Curlplus

{

public:

static  Curlplus& get_instance();

string post(const string& url,const string& content) const;

string get(const string& url) const;

protected:

Curlplus(void);

~Curlplus(void);

Curlplus(const Curlplus&);

Curlplus& operator=(const Curlplus&);

static int writer(char *data, size_t size, size_t nmemb,std::string *writerData);

private:

static auto_ptr<Curlplus> _instance;

inline void _setCurlopt(CURL *curl,const string& url) const;

// default timeout 300s

static const int _defaulttimeout = 300;

static string buffer;

friend class auto_ptr<Curlplus>;

};

}

#endif

Curlpuls.cc文件

#ifndef SPIVOT_CURLPLUS_H__

#include "Curlplus.h"

#endif

using namespace std;

using namespace CP;

auto_ptr<Curlplus> Curlplus::_instance;

string Curlplus::buffer;

static char errorBuffer[CURL_ERROR_SIZE];

Curlplus& Curlplus::get_instance()

{

if(_instance.get() == NULL)

{

_instance.reset(new Curlplus());

}

return *_instance.get();

}

int Curlplus::writer(char *data, size_t size, size_t nmemb, string *writerData)

{

if (writerData == NULL)

return 0;

int len = size*nmemb;

writerData->append(data, len);

return len;

}

Curlplus::Curlplus(void)

{

CURLcode code = curl_global_init(CURL_GLOBAL_ALL);

if(code != CURLE_OK)

{

cout << "curl_init failed, error code is: " << code;

}

}

Curlplus::~Curlplus(void)

{

curl_global_cleanup();

}

string Curlplus::post(const string& url, const string& content) const

{

buffer="";

CURL *curl = curl_easy_init();

if(curl == NULL)

{

cout << "curl_easy_init failed ";

}

_setCurlopt(curl,url);

curl_easy_setopt(curl, CURLOPT_POST, 1 );

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());

CURLcode code = curl_easy_perform(curl);

if(code != CURLE_OK)

{

cout << "curl_easy_perform failed: "<< code;

}

curl_easy_cleanup(curl);

return buffer;

}

string Curlplus::get(const string& url) const

{

buffer="";

CURL *curl = curl_easy_init();

if(curl == NULL)

{

cout << "curl_easy_init failed ";

}

_setCurlopt(curl,url);

CURLcode code = curl_easy_perform(curl);

if(code != CURLE_OK)

{

cout << "curl_easy_perform failed: "<< code;

}

curl_easy_cleanup(curl);

return buffer;

}

void Curlplus::_setCurlopt(CURL *curl,const string& url) const {

curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);

curl_easy_setopt(curl, CURLOPT_HEADER, 0);

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

curl_easy_setopt(curl, CURLOPT_TIMEOUT, _defaulttimeout);

//curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Curlplus::writer);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

}

嵌入式 Linux下curl库API简单介绍的更多相关文章

  1. linux下静态库和动态库一些东西

    http://www.cnblogs.com/changefuture/archive/2011/12/22/2297460.html Linux  动态链接库和静态库示例 文件预览 文件目录树如下, ...

  2. 嵌入式 Linux下永久生效环境变量bashrc

    嵌入式 Linux下永久生效环境变量bashrc 1) .bashrc文件 在linux系统普通用户目录(cd /home/xxx)或root用户目录(cd /root)下,用指令ls -al可以看到 ...

  3. 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH

    谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH  PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...

  4. 嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误

    嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误 2015-05-27 14:19 184人阅读 评论(0) 收藏 举报  分类: 嵌入式(928)  一般察看函数运行时堆栈的 ...

  5. app 下载更新 file-downloader 文件下载库的简单介绍和使用

    app 下载更新 file-downloader 文件下载库的简单介绍和使用 今天介绍一个下载库:file-downloader 文件下载库 说明: * 本文内容来自原 file-downloader ...

  6. 转: 嵌入式linux下usb驱动开发方法--看完少走弯路【转】

    转自:http://blog.csdn.net/jimmy_1986/article/details/5838297 嵌入式linux下的usb属于所有驱动中相当复杂的一个子系统,要想将她彻底征服,至 ...

  7. 转:linux下共享库的注意点之-fpic

    转: http://www.cnblogs.com/leo0000/p/5691483.html linux下共享库的注意点之-fpic 在编译共享库必须加上-fpic.这是为什么呢? 首先看一个简单 ...

  8. 深入理解LINUX下动态库链接器/加载器ld-linux.so.2

    [ld-linux-x86-64.so.2] 最近在Linux 环境下开发,搞了好几天 Compiler 和 linker,觉得有必要来写一篇关于Linux环境下 ld.so的文章了,google上搜 ...

  9. Linux下动态库生成和使用

    Linux下动态库生成和使用 一.动态库的基本概念 1.动态链接库是程序运行时加载的库,当动态链接库正确安装后,所有的程序都可以使用动态库来运行程序.动态链接库是目标文件的集合,目标文件在动态链接库中 ...

随机推荐

  1. 关于CStdioFile的使用问题

    在win32控制台调试如下程序 #include "stdafx.h"#include <afx.h>//#include <iostream>//usin ...

  2. OpenStack学习系列-----第一篇 OpenStack介绍

    刚开始接触OpenStack,被它所承诺的前景,以及现在业界对它的期望吸引(OpenStack被誉为21世纪的Linux开源社区,可以预见其的发展前景是何其广阔.).怎么说呢,我现在也暂时相信,Ope ...

  3. 关于模态/非模态对话框不响应菜单的UPDATE_COMMAND_UI消息(对对WM_INITMENUPOPUP消息的处理)

    对于模态非模态对话框默认是不响应菜单的UPDATE_COMMAND_UI消息的,需要增加对WM_INITMENUPOPUP消息的处理以后,才可以响应UPDATE_COMMAND_UI. void CX ...

  4. WPF之通过EventTrigger修改模板中元素的属性

    前言:对于此操作,我只想说是微软的神经,还是我的笨蛋.为什么EventTrigger就不能像Trigger那样直接设置Property以及Value就对属性进行操作,而必须要放一个Action,而默认 ...

  5. myeclipse 2016 激活,myeclipse 2016 激活

    myeclipse 2016 激活: 找了好久,myeclipse 2016 终于激活了.myeclipse版本是下载的  myeclipse-2016-ci-0-offline-installer- ...

  6. 转载-使用 Feed4JUnit 进行数据与代码分离的 Java 单元测试

    JUnit 是被广泛应用的 Java 单元测试框架,但是它没有很好的提供参数化测试的支持,很多测试人员不得不把测试数据写在程序里或者通过其它方法实现数据与代码的分离,在后续的修改和维护上有诸多限制和不 ...

  7. ftrace的使用【转】

    转自:http://blog.csdn.net/cybertan/article/details/8258394 This article explains how to set up ftrace ...

  8. ubuntu 14.04 与 CentOS 升级GCC/G++至5版本

    # 支持 ubuntu 14.04 add-apt-repository ppa:ubuntu-toolchain-r/test && apt-get update apt-get i ...

  9. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  10. 聚集索引、非聚集索引、聚集索引组织表、堆组织表、Mysql/PostgreSQL对比、联合主键/自增长、InnoDB/MyISAM(引擎方面另开一篇)

    参考了多篇文章,分别记录,如下. 下面是第一篇的总结 http://www.jb51.net/article/76007.htm: 在MySQL中,InnoDB引擎表是(聚集)索引组织表(cluste ...