国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
libcurl庫(kù)介紹


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

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

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

基于 C 的 HTTP 客戶端

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

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

使用 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 根文件。接下來(lái),我定義了兩個(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)用都通過(guò)維護(hù)特定請(qǐng)求狀態(tài)的句柄進(jìn)行操作。這稱為 CURL指針引用。本例還創(chuàng)建一個(gè)特殊的返回碼,稱為 CURLcode。在使用任何 libcurl 函數(shù)之前,您需要調(diào)用 curl_easy_init獲取 CURL句柄。接下來(lái),注意 curl_easy_setopt調(diào)用的數(shù)量。它們?yōu)樘囟ǖ牟僮髋渲镁浔?。?duì)于這些調(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),提交頁(yè)面讀取,最后使用 curl_easy_cleanup清除(當(dāng)使用句柄執(zhí)行完操作后)。

現(xiàn)在看看 write_data函數(shù)。該函數(shù)是針對(duì)特定操作收到數(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)問(wèn)題。否則,它將 cURL 緩沖區(qū)的數(shù)據(jù)復(fù)制到您的緩沖區(qū),并增加索引,指向要寫入的下一個(gè)位置。本例還終止字符串,稍后可以對(duì)其使用 printf。最后,它返回 libcurl 操作的字節(jié)數(shù)量。這將告訴 libcurl 數(shù)據(jù)被提取,它也可以丟棄該數(shù)據(jù)。這就是從網(wǎng)站將文件讀取到內(nèi)存的相對(duì)簡(jiǎn)單的方法。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux使用libcurl提交POST請(qǐng)求
對(duì)CURL的一些研究 - ChinaUnix.net
libcurl
libcurl教程
libcurl使用easy模式阻塞卡死等問(wèn)題的完美解決---超時(shí)設(shè)置
(解決curl_easy_perform阻塞很久的問(wèn)題)libcurl的share interface與curl_easy_perform的性能
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服