CURL的使用

CURL的使用

   libCURL是C语言写的URL下载库,它支持多种协议如FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, IMAP,

   它在多个平台上都能下载,比如Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, Linux, Dos, and more.

 

   它在Windows下以Com的方式使用.

其使用所要调用的函数是:

Curl_easy_init();

Curl_easy_setopt();

Curl_easy_perform();

Curl_easy_getinfo();

Curl_easy_cleanup();

 

 

而其使用之前要做的工作是:

     ::CoInitialize(NULL);

之后要做的工作是:

     ::CoUninitialize();

 

而在下载时,会通过curl_easy_setopt()进行设置用户名,密码,远端下载路径,

头回调函数,写回调函数,进度回调函数。开始执行函数。最后还能调用获取信息函数。

而头回调函数,只是头调用一次,而写回调函数是要反复调用的,进度回调函数也是反复调用的,其是通过curl自身建立的线程调用的。

 

CURL是开源的,可以查看其源代码,

在easy.c中可以看其curl_easy_init()函数,

函数为:

CURL *curl_easy_init(void)

{

  CURLcode res;

  struct SessionHandle *data;

 

 

  if(!initialized) {

    res = curl_global_init(CURL_GLOBAL_DEFAULT);

    if(res) {

     

      DEBUGF(fprintf(stderr, "Error: curl_global_init failed
"));

      return NULL;

    }

  }

 

 

  res = Curl_open(&data);

  if(res != CURLE_OK) {

    DEBUGF(fprintf(stderr, "Error: Curl_open failed
"));

    return NULL;

  }

 

  return data;

}

 

真正的函数全在里面。

对于SessionHandle结构体可以看如下的定义:

struct SessionHandle {

  struct Names dns;

  struct Curl_multi *multi;   

  struct Curl_one_easy *multi_pos;

  struct Curl_share *share;   

  struct SingleRequest req;   

  struct UserDefined set;     

  struct DynamicStatic change;

  struct CookieInfo *cookies; 

  struct Progress progress;   

 

原文地址:https://www.cnblogs.com/hzcya1995/p/13318502.html