2011-09-06 29 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