之前两篇博客介绍了一些最简单的基本知识,今天就来应用一下,写一个HTTP客户端。
以下有些内容,参考出处:
http://www.oschina.net/question/54100_8602
首先包含curl的头文件。
接下来,定义了两个用于传输的变量。第一个变量是 wr_buf,表示将在其中写入传入数据的缓冲区。wr_index表示缓冲区的当前写入索引。
看看 main函数,该函数使用 easy API 进行设置。所有 cURL 调用都通过维护特定请求状态的句柄进行操作。这称为 CURL指针引用。本例还创建一个特殊的返回码,称为 CURLcode。 在使用任何 libcurl 函数之前,您需要调用 curl_easy_init获取 CURL句 柄。接下来,注意 curl_easy_setopt调用的数量。它们为特定的操作配置句柄。对于这些调用,您提供句柄、命令 和选项。首先,本例使用 CURLOPT_URL指定要获取的 URL。然后,它使用 CURL_WRITEDATA提 供一个上下文变量(在本例中,它是内部的 write 错误变量)。最后,它使用 CURLOPT_WRITEFUNCTION指 定数据可用时应该调用的函数。在启动 API 之后,API 将使用它读取的数据多次调用该函数。
要开始传输,调用 curl_easy_perform。它的工作是根据之前的配置执行传输。调用该函数时,在完成传输 或发生错误之前该函数不会返回。main的最后一步是提交返回状态,提交页面读取,最后使用 curl_easy_cleanup清 除(当使用句柄执行完操作后)。
现在看看 write_data函数。该函数是针对特定操作收到数据时调用的回调。注意,当您从网站读取数据时,将写入 该数据(write_data)。将向回调提供一个缓冲区(包含可用数据)、成员数量和大小(缓冲中可用数据总量)、上下文指 针。第一个任务是确保缓冲区(wr_buf)的空间足以写入数据。如果不够,它将设置上下文指针并返回 0,表示出现问题。否则,它将 cURL 缓冲区的数据复制到您的缓冲区,并增加索引,指向要写入的下一个位置。本例还终止字符串,稍后可以对其使用 printf。 最后,它返回 libcurl 操作的字节数量。这将告诉 libcurl 数据被提取,它也可以丢弃该数据。这就是从网站将文件读取到内存的相对简单的方法。
#include <stdio.h>
#include <string.h>
#include "curl.h"
#define MAX_BUF 65536
char wr_buf[MAX_BUF + 1];
int wr_index;
/*
* Write data callback function (called within the context of
* curl_easy_perform.
*/
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
int segsize = size * nmemb;
/* Check to see if this data exceeds the size of our buffer. If so,
* set the user-defined context value and return 0 to indicate a
* problem to curl.
*/
if (wr_index + segsize > MAX_BUF) {
*(int *)userp = 1;
return 0;
}
/* Copy the data from the curl buffer into our buffer */
memcpy((void *)&wr_buf[wr_index], buffer, (size_t)segsize);
/* Update the write index */
wr_index += segsize;
/* Null terminate the buffer */
wr_buf[wr_index] = 0;
/* Return the number of bytes received, indicating to curl that all is okay */
return segsize;
}
/*
* Simple curl application to read the index.html file from a Web site.
*/
int main(void)
{
CURL *curl;
CURLcode ret;
int wr_error;
wr_error = 0;
wr_index = 0;
/* First step, init curl */
curl = curl_easy_init();
if (!curl) {
printf("couldn't init curl ");
return 0;
}
/* Tell curl the URL of the file we're going to retrieve */
curl_easy_setopt(curl, CURLOPT_URL, "www.exampledomain.com");
/* Tell curl that we'll receive data to the function write_data, and
* also provide it with a context pointer for our error return.
*/
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&wr_error);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
/* Allow curl to perform the action */
ret = curl_easy_perform(curl);
printf("ret = %d (write_error = %d) ", ret, wr_error);
/* Emit the page if curl indicates that no errors occurred */
if (ret == 0) printf("%s ", wr_buf);
curl_easy_cleanup(curl);
return 0;
}