2011-06-11 79 views
1

我是新來的使用libcURL並有一個任務執行的問題。連續提交libcURL(C/C++)

我需要做一個兩個連續提交更改文本框。

第一:網絡有一個選擇框(組)和一個提交按鈕。

二:後德先提交您將獲得帶有

我的代碼的特定頁面,但不工作(這兩個的提交):

#include <stdio.h> 
#include <iostream> 
#include <curl/curl.h> 
#include <curl/types.h> 
#include <curl/easy.h> 

int main(void) 
{ 
    CURL *curl; 
    CURLcode res; 

    struct curl_httppost *formpost = NULL; 
    struct curl_httppost *lastptr = NULL; 
    struct curl_slist *headerlist = NULL; 
    static const char buf[] = "Expect:"; 

    curl_global_init(CURL_GLOBAL_ALL); 

    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "group", CURLFORM_COPYCONTENTS, "ImageSource", CURLFORM_END); 
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Select group", CURLFORM_END); 
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "root_ImageSource_I0_Name", CURLFORM_COPYCONTENTS, "weboCam", CURLFORM_END); 
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Save", CURLFORM_END); 

    curl = curl_easy_init(); 
    headerlist = curl_slist_append(headerlist, buf); 

    if(curl) 
    { 
     /* First set the URL that is about to receive our POST. This URL can 
     just as well be a https:// URL if that is what should receive the 
     data. */ 
     curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.90/admin/config.shtml"); 
     curl_easy_setopt(curl, CURLOPT_USERPWD, "xxxx:xxxx"); 
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); 
     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); 

     res = curl_easy_perform(curl); 

     long http_code = 0; 

     curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); 

     if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) 
     { 
      cout << "Todo ha ido perfecto." << endl;   //Succeeded 
     } 
     else 
     { 
      cout << "Ha habido un error." << endl; //Failed 
     } 

     curl_formfree(formpost); 

     curl_easy_cleanup(curl); 
    } 

另一個問題:代碼編譯,並顯示一切正常,但實際上只顯示默認的頁面代碼提前http://192.168.0.90/admin/config.shtml

感謝

+0

我不明白。如果你想要兩次提交,你的程序爲什麼只調用一次curl_easy_perform()? – 2011-06-11 22:20:28

+0

因爲首先我想看看前兩個curl_formadd的工作原理。在原始代碼中,最後兩個curl_formadd被註釋。 – 2011-06-12 06:41:45

回答

1

第一步完成。

此代碼完成第一步。

char *data1="group=ImageSource"; 
    char *data2="root_ImageSource_I0_Name=weboCam"; 

    curl = curl_easy_init(); 

    if(curl) 
    { 
     // Put the original page 
     curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.90/admin/config.shtml"); 
     curl_easy_setopt(curl, CURLOPT_USERPWD, "root:root");  
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data1); 

選擇第一個選擇。 enter image description here

這樣做後我想修改該文本框 enter image description here

總結一下:我想在一個頁面的第二級來改變文本框。問題是在第二個easy_perform返回到頂級libcurl。