2011-09-06 33 views
1

我之前只有一點點觸及libcurl,但現在我必須進入頁面並登錄。這意味着我必須填寫一個用戶名,密碼在c中形成libcurl

我一直在尋找一個好幾天的例子,但沒有任何工作,沒有任何解釋讓我明白我應該做什麼。

在這個例子中,他們發送一個表單,但然後呢?我如何獲得返回的信息? http://curl.haxx.se/libcurl/c/http-post.html

在下面的鏈接中,我發現有人有類似的問題。不知道他是否解決了他的問題。我偷了CURLOPT_FOLLOWLOCATION行,但我無法到達更新的頁面。 (我假設我會在瀏覽器中獲得我得到的信息,但不會)。在這個頁面上,他們也開始談論捲曲處理餅乾。我認爲cookie被用來記住用戶名以簡化登錄。 How do I use libcurl to login to a secure website and get at the html behind the login

我能想到的最簡單的嘗試就是轉到此頁面並在下面的頁面上提交vehicle = Car。當我在瀏覽器中執行該操作時,頁面會返回「Input was received as: vehicle = Car」,但我不會在使用curl的c/C++程序中獲得它。 http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox

下面是我使用的代碼的一部分:

CURL *curl; 
CURLcode res; 
curl = curl_easy_init(); 


if(curl) { 
    printf("Open %s\n", name.c_str()); 

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox"); 
    curl_easy_perform(curl); 

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_easy_setopt(curl, CURLOPT_POST, 1); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "vehicle=Car"); 
    curl_easy_setopt(curl, CURLOPT_URL, name.c_str()); 
    curl_easy_perform(curl); 

    int i = 0; 
    do{ 
     if (i) 
     { 
      curl_easy_cleanup(curl); 
      curl_easy_setopt(curl, CURLOPT_URL, name.c_str()); 
     } 
     printf("Try number %d \n", ++i); 
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &localStore); 
     res = curl_easy_perform(curl); 
    } while(res); 

    curl_easy_cleanup(curl); 
+0

localStore()的代碼在哪裏?前兩次調用curl_easy_perform(curl)會怎樣? –

+0

對curl_easy_setopt()的調用設置CURLOPT_WRITEDATA的地方是什麼?當你進入回調函數時,你看到了什麼?最後,「正常回調函數」是什麼意思?只有其中一個。 –

+0

從第一次打電話到curl_easy_perform我不知道我的預期。它只是我嘗試使用它的100次嘗試之一。我第二次調用這個函數,我期望表單發佈,而不是。 – slutbit

回答

0

你必須建立一個回調函數:

... 
curl_res = curl_easy_setopt(handle, CURLOPT_WRITEDATA, ptr); 
curl_res = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, callback); 
... 

當你進入callback(),輸入的數據是可在ptr上獲得。

不要忘記從callback()退出時返回realsize