- 論壇徽章:
- 0
|
就是那個(gè)simple.c,代碼如下:- #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://www.163.com");
- res = curl_easy_perform(curl);
-
- /* always cleanup */
- curl_easy_cleanup(curl);
- }
- return 0;
- }
復(fù)制代碼 simple.c shows how to get a remote web page in only four libcurl function calls.
simple.c 這個(gè)例子告訴你,僅僅需要4個(gè)libcurl函數(shù),就能獲取一個(gè)網(wǎng)頁(yè)。
執(zhí)行沒(méi)有任何結(jié)果,必須寫(xiě)成回調(diào)的方式,才能打印輸出,按照文檔里說(shuō)的,這個(gè)例子似乎會(huì)輸出到stdout的。
文檔libcurl-tutorial - libcurl programming tutorial 里有如下一段:
libcurl offers its own default internal callback that will take care of the data if you don't set the callback with CURLOPT_WRITEFUNCTION. It will then simply output the received data to stdout. You can have the default callback write the data to a different file handle by passing a 'FILE *' to a file opened for writing with the CURLOPT_WRITEDATA option.
以下是回調(diào)的寫(xiě)法:- #include <stdio.h>
- #include <curl/curl.h>
-
- size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
- {
- printf("%s",(char*)buffer);
-
- return size*nmemb;
- }
-
- int main(void)
- {
- CURL *curl;
- CURLcode res;
- char buf[2048] = {0};
-
- curl_global_init(CURL_GLOBAL_DEFAULT);
- curl = curl_easy_init();
-
- if(curl) {
- curl_easy_setopt(curl, CURLOPT_URL, "http://www.163.com");
-
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-
- // curl_easy_setopt
- res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- if(res != CURLE_OK){
- printf("curl_easy_setopt not return CURLE_OK\n");
- }
- else{
- printf("curl_easy_setopt exec success\n");
- }
-
- // curl_easy_setopt
-
- // curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
- // curl_easy_perform
- res = curl_easy_perform(curl);
- if(res != CURLE_OK){
- printf("curl_easy_perform not return CURLE_OK\n");
- }
- else{
- printf("curl_easy_perform exec success\n");
- }
-
- // printf("buf:%s\n", buf);
-
- /* always cleanup */
- curl_easy_cleanup(curl);
- }
- return 0;
- }
復(fù)制代碼 |
|