文章目錄
- 一、簡介
- 二、常用的基本函數
- 2.1 CURLcode curl_global_init(long flags);
- 2.2 void curl_global_cleanup(void)
- 2.3 char *curl_version( )
- 2.4 CURL *curl_easy_init( );
- 2.5 void curl_easy_cleanup(CURL *handle);
- 2.6 CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
- 2.7 CURLcode curl_easy_perform(CURL *handle)
- 三、curl_easy_setopt函數部分選項
- 3.1 CURLOPT_URL
- 3.2 CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA
- 3.3 CURLOPT_HEADERFUNCTION,CURLOPT_HEADERDATA
- 3.4 CURLOPT_READFUNCTION CURLOPT_READDATA
- 3.5 CURLOPT_NOPROGRESS,CURLOPT_PROGRESSFUNCTION,CURLOPT_PROGRESSDATA
- 3.6 URLOPT_TIMEOUT,CURLOPT_CONNECTIONTIMEOUT
- 3.7 CURLOPT_FOLLOWLOCATION
- 3.8 CURLOPT_RANGE: CURLOPT_RESUME_FROM:
- 四、curl_easy_perform 函數說明(error 狀態碼)
參考:
http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html
一、簡介
libcurl是一個跨平臺的網絡協議庫,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 協議。libcurl同樣支持HTTPS證書授權,HTTP POST, HTTP PUT, FTP 上傳, HTTP基本表單上傳,代理,cookies,和用戶認證。想要知道更多關於libcurl的介紹,可以到官網 http://curl.haxx.se/上去了解,在這裏不再詳述。
下載地址:http://curl.freeby.pctools.cl/download/
在基於LibCurl的程序裏,主要採用callback function (回調函數)的形式完成傳輸任務,用戶在啓動傳輸前設置好各類參數和回調函數,當滿足條件時libcurl將調用用戶的回調函數實現特定功能。
- 調用curl_global_init()初始化libcurl
- 調用curl_easy_init()函數得到 easy interface型指針
- 調用curl_easy_setopt()設置傳輸選項
- 根據curl_easy_setopt()設置的傳輸選項,實現回調函數以完成用戶特定任務
- 調用curl_easy_perform()函數完成傳輸任務
- 調用curl_easy_cleanup()釋放內存
在整過過程中設置curl_easy_setopt()參數是最關鍵的,幾乎所有的libcurl程序都要使用它。
二、常用的基本函數
2.1 CURLcode curl_global_init(long flags);
- 描述:
這個函數只能用一次。(其實在調用curl_global_cleanup 函數後仍然可再用)
如果這個函數在curl_easy_init函數調用時還沒調用,它將由libcurl庫自動調用,所以多線程下最好主動調用該函數以防止在線程中curl_easy_init時多次調用。
注意:雖然libcurl是線程安全的,但curl_global_init是不能保證線程安全的,所以不要在每個線程中都調用curl_global_init,應該將該函數的調用放在主線程中。 - 參數:flags
CURL_GLOBAL_ALL //初始化所有的可能的調用。
CURL_GLOBAL_SSL //初始化支持 安全套接字層。
CURL_GLOBAL_WIN32 //初始化win32套接字庫。
CURL_GLOBAL_NOTHING //沒有額外的初始化。
2.2 void curl_global_cleanup(void)
- 描述:
在結束libcurl使用的時候,用來對curl_global_init做的工作清理。類似於close的函數。
注意:雖然libcurl是線程安全的,但curl_global_cleanup是不能保證線程安全的,所以不要在每個線程中都調用curl_global_init,應該將該函數的調用放在主線程中。
2.3 char *curl_version( )
- 描述:
打印當前libcurl庫的版本。
2.4 CURL *curl_easy_init( );
- 描述:
curl_easy_init用來初始化一個CURL的指針(有些像返回FILE類型的指針一樣). 相應的在調用結束時要用curl_easy_cleanup函數清理.
一般curl_easy_init意味着一個會話的開始. 它會返回一個easy_handle(CURL*對象), 一般都用在easy系列的函數中.
2.5 void curl_easy_cleanup(CURL *handle);
- 描述:
這個調用用來結束一個會話.與curl_easy_init配合着用. - 參數:
CURL類型的指針.
2.6 CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
- 描述:
這個函數最重要了.幾乎所有的curl 程序都要頻繁的使用它.它告訴curl庫.程序將有如何的行爲. 比如要查看一個網頁的html代碼等.(這個函數有些像ioctl函數) - 參數:
(1)CURL類型的指針
(2)各種CURLoption類型的選項.(都在curl.h庫裏有定義,man 也可以查看到)
(3) parameter 這個參數 既可以是個函數的指針,也可以是某個對象的指針,也可以是個long型的變量.它用什麼這取決於第二個參數. - CURLoption 這個參數的取值很多.具體的可以查看man手冊.
2.7 CURLcode curl_easy_perform(CURL *handle)
- 描述:
這個函數在初始化CURL類型的指針 以及curl_easy_setopt完成後調用. 就像字面的意思所說perform就像是個舞臺.讓我們設置的option 運作起來. - 參數:
CURL類型的指針.
三、curl_easy_setopt函數部分選項
3.1 CURLOPT_URL
設置訪問URL
3.2 CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA
回調函數原型爲:size_t function( void *ptr, size_t size, size_t nmemb, void *stream); 函數將在libcurl接收到數據後被調用,因此函數多做數據保存的功能,如處理下載文件。
- CURLOPT_WRITEDATA 用於表明CURLOPT_WRITEFUNCTION函數中的stream指針的來源。
- 如果你沒有通過CURLOPT_WRITEFUNCTION屬性給easy handle設置回調函數,libcurl會提供一個默認的回調函數,它只是簡單的將接收到的數據打印到標準輸出。你也可以通過 CURLOPT_WRITEDATA屬性給默認回調函數傳遞一個已經打開的文件指針,用於將數據輸出到文件裏。
3.3 CURLOPT_HEADERFUNCTION,CURLOPT_HEADERDATA
回調函數原型爲 size_t function( void *ptr, size_t size,size_t nmemb, void *stream); libcurl一旦接收到http 頭部數據後將調用該函數。
- CURLOPT_WRITEDATA 傳遞指針給libcurl,該指針表明CURLOPT_HEADERFUNCTION 函數的stream指針的來源。
3.4 CURLOPT_READFUNCTION CURLOPT_READDATA
libCurl需要讀取數據傳遞給遠程主機時將調用CURLOPT_READFUNCTION指定的函數,函數原型是:size_t function(void *ptr, size_t size, size_t nmemb,void *stream).
- CURLOPT_READDATA 表明CURLOPT_READFUNCTION函數原型中的stream指針來源。
3.5 CURLOPT_NOPROGRESS,CURLOPT_PROGRESSFUNCTION,CURLOPT_PROGRESSDATA
跟數據傳輸進度相關的參數。CURLOPT_PROGRESSFUNCTION 指定的函數正常情況下每秒被libcurl調用一次,爲了使CURLOPT_PROGRESSFUNCTION被調用,CURLOPT_NOPROGRESS必須被設置爲false,CURLOPT_PROGRESSDATA指定的參數將作爲CURLOPT_PROGRESSFUNCTION指定函數的第一個參數
3.6 URLOPT_TIMEOUT,CURLOPT_CONNECTIONTIMEOUT
CURLOPT_TIMEOUT 由於設置傳輸時間,CURLOPT_CONNECTIONTIMEOUT 設置連接等待時間
3.7 CURLOPT_FOLLOWLOCATION
設置重定位URL
3.8 CURLOPT_RANGE: CURLOPT_RESUME_FROM:
斷點續傳相關設置。
- CURLOPT_RANGE 指定char *參數傳遞給libcurl,用於指明http域的RANGE頭域。
- CURLOPT_RESUME_FROM 傳遞一個long參數給libcurl,指定你希望開始傳遞的偏移量。
四、curl_easy_perform 函數說明(error 狀態碼)
- CURLE_OK
任務完成一切都好 - CURLE_UNSUPPORTED_PROTOCOL
不支持的協議,由URL的頭部指定 - CURLE_COULDNT_CONNECT
不能連接到remote 主機或者代理 - CURLE_REMOTE_ACCESS_DENIED
訪問被拒絕 - CURLE_HTTP_RETURNED_ERROR
Http返回錯誤 - CURLE_READ_ERROR
五、libcurl使用的HTTP消息頭
當使用libcurl發送http請求時,它會自動添加一些http頭。我們可以通過CURLOPT_HTTPHEADER屬性手動替換、添加或刪除相應 的HTTP消息頭。
- Host
http1.1(大部分http1.0)版本都要求客戶端請求提供這個信息頭。 - Pragma
“no-cache”。表示不要緩衝數據。 - Accept
“/”。表示允許接收任何類型的數據。 - Expect
以POST的方式向HTTP服務器提交請求時,libcurl會設置該消息頭爲"100-continue",它要求服務器在正式處理該請求之前,返回一 個"OK"消息。如果POST的數據很小,libcurl可能不會設置該消息頭。 - 自定義選項
curl_easy_setopt(easy_handle, CURLOPT_CUSTOMREQUEST, "MYOWNREQUEST");
- 修改消息頭
struct curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Hey-server-hey: how are you?");
headers = curl_slist_append(headers, "X-silly-content: yes");
/* pass our list of custom made headers */
curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);
curl_easy_perform(easyhandle); /* transfer http */
curl_slist_free_all(headers); /* free the header list */
- 重新設置消息頭
headers = curl_slist_append(headers, "Accept: Agent-007");
headers = curl_slist_append(headers, "Host: munged.host.line");
- 刪除消息頭
對於一個已經存在的消息頭,設置它的內容爲空,libcurl在發送請求時就不會同時提交該消息頭:
headers = curl_slist_append(headers, "Accept:");
六、獲取http應答頭信息
發出http請求後,服務器會返回應答頭信息和應答數據,如果僅僅是打印應答頭的所有內容,則直接可以通過curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, 打印函數)的方式來完成,這裏需要獲取的是應答頭中特定的信息,比如應答碼、cookies列表等,則需要通過下面這個函數:
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... );
info參數就是我們需要獲取的內容,下面是一些參數值:
(1)CURLINFO_RESPONSE_CODE
獲取應答碼
(2)CURLINFO_HEADER_SIZE
頭大小
(3)CURLINFO_COOKIELIST
cookies列表
除了獲取應答信息外,這個函數還能獲取curl的一些內部信息,如請求時間、連接時間等等。
七、關於密碼
- 通過CURLOPT_USERPWD屬性來設置用戶名與密碼。參數是格式如 “user:password ”的字符串:
curl_easy_setopt(easy_handle, CURLOPT_USERPWD, "user_name:password");
- 有時候在訪問代理服務器的時候,可能時時要求提供用戶名和密碼進行用戶身份驗證。這種情況下,libcurl提供了另 一個屬性CURLOPT_PROXYUSERPWD:
curl_easy_setopt(easy_handle, CURLOPT_PROXYUSERPWD, "user_name:password");
- 在UNIX平臺下,訪問FTP的用戶名和密碼可能會被保存在$HOME/.netrc文件中。libcurl支持直接從這個文件中獲取用戶名與密碼:
curl_easy_setopt(easy_handle, CURLOPT_NETRC, 1L);
- 在使用SSL時,可能需要提供一個私鑰用於數據安全傳輸,通過CURLOPT_KEYPASSWD來設置私鑰:
curl_easy_setopt(easy_handle, CURLOPT_KEYPASSWD, "keypassword");
八、HTTP驗證
在使用HTTP協議時,客戶端有很多種方式向服務器提供驗證信息。默認的 HTTP驗證方法是"Basic”,它將用戶名與密碼以明文的方式、經Base64編碼後保存在HTTP請求頭中,發往服務器。當然這不太安全。當前版本的libcurl支持的驗證方法有:basic, Digest, NTLM, Negotiate, GSS-Negotiate and SPNEGO。
- CURLOPT_HTTPAUTH屬性來設置具體 的驗證方式:
curl_easy_setopt(easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
- 向代理服務器發送驗證信息時,可以通過CURLOPT_PROXYAUTH設置驗證方式:
curl_easy_setopt(easy_handle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
- 也可以同時設置多種驗證方式(通過按位與), 使用‘CURLAUTH_ANY‘將允許libcurl可以選擇任何它所支持的驗證方式。通過CURLOPT_HTTPAUTH或 CURLOPT_PROXYAUTH屬性設置的多種驗證方式,libcurl會在運行時選擇一種它認爲是最好的方式與服務器通信:
curl_easy_setopt(easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
// curl_easy_setopt(easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY)
九、常用例子
9.1 基本的http GET/POST操作
#include <stdio.h>
#include <curl/curl.h>
bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回結果用文件存儲
return false;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改協議頭
curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //將返回的http頭輸出到fp指向的文件
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //將返回的html主體數據輸出到fp指向的文件
res = curl_easy_perform(curl); // 執行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
return false;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post內容
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
curl_easy_setopt(curl, CURLOPT_URL, " http://mail.sina.com.cn/cgi-bin/login.cgi "); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
int main(void)
{
getUrl("/tmp/get.html");
postUrl("/tmp/post.html");
}
9.2 獲取html網頁
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
CURL *curl; //定義CURL類型的指針
CURLcode res; //定義CURLcode類型的變量,保存返回狀態碼
if(argc!=2)
{
printf("Usage : file <url>;\n");
exit(1);
}
curl = curl_easy_init(); //初始化一個CURL類型的指針
if(curl!=NULL)
{
//設置curl選項. 其中CURLOPT_URL是讓用戶指 定url. argv[1]中存放的命令行傳進來的網址
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
//調用curl_easy_perform 執行我們的設置.並進行相關的操作. 在這 裏只在屏幕上顯示出來.
res = curl_easy_perform(curl);
//清除curl操作.
curl_easy_cleanup(curl);
}
return 0;
}
9.3 網頁下載保存實例
// 採用CURLOPT_WRITEFUNCTION 實現網頁下載保存功能
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <curl/curl.h>;
#include <curl/types.h>;
#include <curl/easy.h>;
FILE *fp; //定義FILE類型指針
//這個函數是爲了符合CURLOPT_WRITEFUNCTION而構造的
//完成數據保存功能
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr, size, nmemb, (FILE *)fp);
return written;
}
int main(int argc, char *argv[])
{
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl=curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
if((fp=fopen(argv[2],"w"))==NULL)
{
curl_easy_cleanup(curl);
exit(1);
}
////CURLOPT_WRITEFUNCTION 將後繼的動作交給write_data函數處理
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
exit(0);
}
9.4 進度條實例顯示文件下載進度
// 採用CURLOPT_NOPROGRESS, CURLOPT_PROGRESSFUNCTION CURLOPT_PROGRESSDATA 實現文件傳輸進度提示功能
//函數採用了gtk庫,故編譯時需指定gtk庫
//函數啓動專門的線程用於顯示gtk 進度條bar
#include <stdio.h>
#include <gtk/gtk.h>
#include <curl/curl.h>
#include <curl/types.h> /* new for v7 */
#include <curl/easy.h> /* new for v7 */
GtkWidget *Bar;
////這個函數是爲了符合CURLOPT_WRITEFUNCTION而構造的
//完成數據保存功能
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
//這個函數是爲了符合CURLOPT_READFUNCTION而構造的
//數據上傳時使用
size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fread(ptr, size, nmemb, stream);
}
//這個函數是爲了符合CURLOPT_PROGRESSFUNCTION而構造的
//顯示文件傳輸進度,t代表文件大小,d代表傳 輸已經完成部分
int my_progress_func(GtkWidget *bar,
double t, /* dltotal */
double d, /* dlnow */
double ultotal,
double ulnow)
{
/* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
gdk_threads_enter();
gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
gdk_threads_leave();
return 0;
}
void *my_thread(void *ptr)
{
CURL *curl;
CURLcode res;
FILE *outfile;
gchar *url = ptr;
curl = curl_easy_init();
if(curl)
{
outfile = fopen("test.curl", "w");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
res = curl_easy_perform(curl);
fclose(outfile);
/* always cleanup */
curl_easy_cleanup(curl);
}
return NULL;
}
int main(int argc, char **argv)
{
GtkWidget *Window, *Frame, *Frame2;
GtkAdjustment *adj;
/* Must initialize libcurl before any threads are started */
curl_global_init(CURL_GLOBAL_ALL);
/* Init thread */
g_thread_init(NULL);
gtk_init(&argc, &argv);
Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
gtk_container_add(GTK_CONTAINER(Window), Frame);
Frame2 = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
gtk_container_add(GTK_CONTAINER(Frame), Frame2);
gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
Bar = gtk_progress_bar_new_with_adjustment(adj);
gtk_container_add(GTK_CONTAINER(Frame2), Bar);
gtk_widget_show_all(Window);
if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
g_warning("can't create the thread");
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return 0;
}
9.5 斷點續傳實例
//採用CURLOPT_RESUME_FROM_LARGE 實現文件斷點續傳功能
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <curl/curl.h>
//這個函數爲CURLOPT_HEADERFUNCTION參數構造
/* 從http頭部獲取文件size*/
size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {
int r;
long len = 0;
/* _snscanf() is Win32 specific */
// r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
r = sscanf(ptr, "Content-Length: %ld\n", &len);
if (r) /* Microsoft: we don't read the specs */
*((long *) stream) = len;
return size * nmemb;
}
/* 保存下載文件 */
size_t wirtefunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
/*讀取上傳文件 */
size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
{
FILE *f = stream;
size_t n;
if (ferror(f))
return CURL_READFUNC_ABORT;
n = fread(ptr, size, nmemb, f) * size;
return n;
}
// 下載 或者上傳文件函數
int download(CURL *curlhandle, const char * remotepath, const char * localpath,
long timeout, long tries)
{
FILE *f;
curl_off_t local_file_len = -1 ;
long filesize =0 ;
CURLcode r = CURLE_GOT_NOTHING;
int c;
struct stat file_info;
int use_resume = 0;
/* 得到本地文件大小 */
//if(access(localpath,F_OK) ==0)
if(stat(localpath, &file_info) == 0)
{
local_file_len = file_info.st_size;
use_resume = 1;
}
//採用追加方式打開文件,便於實現文件斷點續傳工作
f = fopen(localpath, "ab+");
if (f == NULL) {
perror(NULL);
return 0;
}
//curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout); // 設置連接超時,單位秒
//設置http 頭部處理函數
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);
// 設置文件續傳的位置給libcurl
curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, wirtefunc);
//curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
//curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
r = curl_easy_perform(curlhandle);
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n", curl_easy_strerror(r));
return 0;
}
}
int main(int c, char **argv) {
CURL *curlhandle = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curlhandle = curl_easy_init();
//download(curlhandle, "ftp://user:pass@host/path/file", "C:\\file", 0, 3);
download(curlhandle , "http://software.sky-union.cn/index.asp","/work/index.asp",1,3);
curl_easy_cleanup(curlhandle);
curl_global_cleanup();
return 0;
}
9.6 輸出結果保存爲字符串
size_t WriteData(void *ptr, size_t size, size_t nmemb, void stream){
std::string data((const char*)ptr, (size_t) size*nmemb);
*((std::stringstream*) stream) << data << std::endl;
return size * nmemb;
}
int main(){
CURL* curl = curl_easy_init();
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
std::stringstream out;
if(curl){
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json:charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, json.length());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
res = curl_easy_perform(curl);
json_out = out.str();
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}