文章3:libcurl基本编程概述

转载请注明出处http://blog.csdn.net/yankai0219/article/details/8159697
0.序
1.简单用例
2.论述说明
3.更为复杂的例子
4.总结

0.序
     前一段时间,因为项目需要,恶补了一番libcurl的内容,故记录下来。至于安装啥的就请大家参阅官方文档。http://curl.haxx.se/docs/install.html
     任何时候学习任何东西,官方的文档绝对是最好的教程。http://curl.haxx.se/libcurl/c/ 
1.简单案例
     下面首先给出一个官方的基本示例,该示例中使用了几个easy interface。我将其称为 基本用例
     
#include <stdio.h>
#include <curl/curl.h>
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
 
    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

2.论述说明:
          正如在译文 Using The libcurl C Interface(重要)讲到的,easy interface的步骤主要是:初始化、设置选项、执行。
         1) 初始化: curl_easy_init
          作用:这个函数安装libcurl所需的程序环境。可以将其认为是库加载器。即:使用该函数加载库文件
         2) 设置选项: curl_easy_setopt
          作用:用于告诉libcurl如何操作。通过使用恰当的参数,你可以使用curl_easy_setopt改变libcurl的行为。
          3)执行: curl_easy_perform
          作用:用于执行整个操作
3.更为复杂的例子
          在示例中,有一个例子名为 postit2.c,展示了如何构建RFC1867格式的POST形式并且将其发送给服务器。关于 RFC1867格式的POST形式的介绍请看文章4:multipart/form-data详细介绍。
          通过该示例,我们可以发现postit2.c与上面中 基本用例结构相同,只是多了部分内容:多出了curl_formadd和几个curl_easy_setopt选项。
          我们可以 笼统的概括libcurl编程为:基本用例+其他HTTP请求内容,二者融合为libcurl编程。
 
4.总结:
          easy interface使用比较简单,但是必须熟悉使用curl_easy_setopt函数的选项的含义,以及基本的HTTP协议的内容。
     

     
     

你可能感兴趣的:(文章3:libcurl基本编程概述)