小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

libcurl庫介紹

 lao_o 2010-07-23


libcurl庫是一個(gè)實(shí)現(xiàn)了各種客戶端協(xié)議的網(wǎng)絡(luò)編程庫。目前它支持12種以上的協(xié)議,包括 FTP、HTTP、Telnet以及其他安全變體。

如果您有 10 年以上的腳本語言經(jīng)驗(yàn),您就會(huì)注意到它們的標(biāo)記有很大的變化。Python、Ruby、Perl 等這些腳本語言不僅包含套接字層(C 或 C++ 中也有),還包含了應(yīng)用層協(xié)議 API。這些腳本語言合并了高級功能,可以創(chuàng)建 HTTP 服務(wù)器或客戶端。libcurl 庫為 C 和 C++ 之類的語言添加了類似的功能,但是它可以在不同的語言之間移植。在所有它支持的語言中都能找到與 libcurl 相當(dāng)?shù)男袨?,但是由于這些語言的差異很大(設(shè)想一下 C 和 Scheme),提供這些行為的方式也很不相同。

libcurl 庫以 API 的形式封裝各種客戶端協(xié)議,因此它可以被高級語言使用(如今已超過 30 種)。下面的示例研究使用 C 構(gòu)建的簡單 HTTP 客戶端(適合構(gòu)建 Web 爬行器)。

基于 C 的 HTTP 客戶端

C API 在 libcurl 功能上提供了兩個(gè) API。easy 接口是一個(gè)簡單的同步 API(意味著當(dāng)您使用請求調(diào)用 libcurl 時(shí),將能夠滿足您的請求,直到完成或發(fā)生錯(cuò)誤)。多接口可以進(jìn)一步控制 libcurl,您的應(yīng)用程序可以執(zhí)行多個(gè)同步傳輸,并控制 libcurl 何時(shí)何地移動(dòng)數(shù)據(jù)。

該示例使用 easy 接口。該 API 還能控制數(shù)據(jù)移動(dòng)過程(使用回調(diào)),但正如其名稱所示,使用起來非常簡單。清單 3 提供了 HTTP 的 C 語言示例。

使用 libcurl easy 接口的 C HTTP 客戶端

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <curl/curl.h> 
  4.  
  5. #define MAX_BUF      65536 
  6.  
  7. char wr_buf[MAX_BUF+1]; 
  8. int  wr_index; 
  9.  
  10. /*
  11.  * Write data callback function (called within the context of
  12.  * curl_easy_perform.
  13.  */ 
  14. size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) 
  15.     int segsize = size * nmemb; 
  16.  
  17.     /* Check to see if this data exceeds the size of our buffer. If so,
  18.      * set the user-defined context value and return 0 to indicate a
  19.      * problem to curl.
  20.      */ 
  21.     if ( wr_index + segsize > MAX_BUF ) { 
  22.         *(int *)userp = 1; 
  23.         return 0; 
  24.     } 
  25.  
  26.     /* Copy the data from the curl buffer into our buffer */ 
  27.     memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize ); 
  28.  
  29.     /* Update the write index */ 
  30.     wr_index += segsize; 
  31.  
  32.     /* Null terminate the buffer */ 
  33.     wr_buf[wr_index] = 0; 
  34.  
  35.     /* Return the number of bytes received, indicating to curl that all is okay */ 
  36.     return segsize; 
  37.  
  38.  
  39. /*
  40.  * Simple curl application to read the index.html file from a Web site.
  41.  */ 
  42. int main( void ) 
  43.     CURL *curl; 
  44.     CURLcode ret; 
  45.     int  wr_error; 
  46.  
  47.     wr_error = 0; 
  48.     wr_index = 0; 
  49.  
  50.     /* First step, init curl */ 
  51.     curl = curl_easy_init(); 
  52.     if (!curl) { 
  53.         printf("couldn't init curl "); 
  54.         return 0; 
  55.     } 
  56.  
  57.     /* Tell curl the URL of the file we're going to retrieve */ 
  58.     curl_easy_setopt( curl, CURLOPT_URL, "www.exampledomain.com" ); 
  59.  
  60.     /* Tell curl that we'll receive data to the function write_data, and
  61.      * also provide it with a context pointer for our error return.
  62.      */ 
  63.     curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&wr_error ); 
  64.     curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data ); 
  65.  
  66.     /* Allow curl to perform the action */ 
  67.     ret = curl_easy_perform( curl ); 
  68.  
  69.     printf( "ret = %d (write_error = %d) ", ret, wr_error ); 
  70.  
  71.     /* Emit the page if curl indicates that no errors occurred */ 
  72.     if ( ret == 0 ) printf( "%s ", wr_buf ); 
  73.  
  74.     curl_easy_cleanup( curl ); 
  75.  
  76.     return 0; 
  77. }

最上方是必需的 include文件,包括 cURL 根文件。接下來,我定義了兩個(gè)用于傳輸?shù)淖兞?。第一個(gè)變量是 wr_buf,表示將在其中寫入傳入數(shù)據(jù)的緩沖區(qū)。wr_index表示緩沖區(qū)的當(dāng)前寫入索引。

轉(zhuǎn)到 main函數(shù),該函數(shù)使用 easy API 進(jìn)行設(shè)置。所有 cURL 調(diào)用都通過維護(hù)特定請求狀態(tài)的句柄進(jìn)行操作。這稱為 CURL指針引用。本例還創(chuàng)建一個(gè)特殊的返回碼,稱為 CURLcode。在使用任何 libcurl 函數(shù)之前,您需要調(diào)用 curl_easy_init獲取 CURL句柄。接下來,注意 curl_easy_setopt調(diào)用的數(shù)量。它們?yōu)樘囟ǖ牟僮髋渲镁浔τ谶@些調(diào)用,您提供句柄、命令和選項(xiàng)。首先,本例使用 CURLOPT_URL指定要獲取的 URL。然后,它使用 CURL_WRITEDATA提供一個(gè)上下文變量(在本例中,它是內(nèi)部的 write 錯(cuò)誤變量)。最后,它使用 CURLOPT_WRITEFUNCTION指定數(shù)據(jù)可用時(shí)應(yīng)該調(diào)用的函數(shù)。在啟動(dòng) API 之后,API 將使用它讀取的數(shù)據(jù)多次調(diào)用該函數(shù)。

要開始傳輸,調(diào)用 curl_easy_perform。它的工作是根據(jù)之前的配置執(zhí)行傳輸。調(diào)用該函數(shù)時(shí),在完成傳輸或發(fā)生錯(cuò)誤之前該函數(shù)不會(huì)返回。main的最后一步是提交返回狀態(tài),提交頁面讀取,最后使用 curl_easy_cleanup清除(當(dāng)使用句柄執(zhí)行完操作后)。

現(xiàn)在看看 write_data函數(shù)。該函數(shù)是針對特定操作收到數(shù)據(jù)時(shí)調(diào)用的回調(diào)。注意,當(dāng)您從網(wǎng)站讀取數(shù)據(jù)時(shí),將寫入該數(shù)據(jù)(write_data)。將向回調(diào)提供一個(gè)緩沖區(qū)(包含可用數(shù)據(jù))、成員數(shù)量和大?。ň彌_中可用數(shù)據(jù)總量)、上下文指針。第一個(gè)任務(wù)是確保緩沖區(qū)(wr_buf)的空間足以寫入數(shù)據(jù)。如果不夠,它將設(shè)置上下文指針并返回 0,表示出現(xiàn)問題。否則,它將 cURL 緩沖區(qū)的數(shù)據(jù)復(fù)制到您的緩沖區(qū),并增加索引,指向要寫入的下一個(gè)位置。本例還終止字符串,稍后可以對其使用 printf。最后,它返回 libcurl 操作的字節(jié)數(shù)量。這將告訴 libcurl 數(shù)據(jù)被提取,它也可以丟棄該數(shù)據(jù)。這就是從網(wǎng)站將文件讀取到內(nèi)存的相對簡單的方法。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多