在c中形成libcurl
我之前只接触过 libcurl 一点点,但现在我必须进入一个页面并登录。这意味着我必须填写一张包含用户名、密码的表格
,我已经搜索了好几天的示例,但没有任何效果,没有任何解释让我明白我应该做什么。
在这个例子中,他们发送了一个表格,但是然后呢?如何获取返回的信息? http://curl.haxx.se/libcurl/c/http-post。 html
在下面的链接上我发现有人有类似的问题。不知道他是否解决了他的问题。我偷了 CURLOPT_FOLLOWLOCATION 行,但无法到达更新的页面。 (我想如果我在浏览器中执行此操作,我会得到我得到的信息,但不会)。在此页面上,他们还开始讨论curl 处理cookie。我认为 cookie 是用来记住用户名以简化登录的。 如何使用 libcurl 登录到安全网站并获取登录后的 html
我能想到的最简单的尝试就是转到此页面并在页面上提交vehicle=Car以下。当我在浏览器中执行此操作时,页面返回“输入接收为: 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);
Ive only touched libcurl a little before, but now i have to get to a page and login. This means i have to fill in a form with user name, password
Ive been searching for an example for days now, but nothing works, no explanations makes me understand what i am supposed to do.
In this example they send a form, but then what? How do I reach the information returned?
http://curl.haxx.se/libcurl/c/http-post.html
On the following link I found somebody that had a similar problem. No idea if he solved his problems. I stole the CURLOPT_FOLLOWLOCATION-line but i could not reached the updated page. (I assume I would get the information I get if I do it in a browser, but nope). On this page they also started talking about curl handling cookies. I thought cookies were used to remember usernames to simplify login.
How do I use libcurl to login to a secure website and get at the html behind the login
The most simple attempt I could think of was to just go to this page and submit vehicle=Car on the page below. When i do it in a browser the page returns "Input was received as:
vehicle=Car", but I do not get it in my c/c++-program using curl.
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox
Below is a part of the code I am using:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须设置一个回调函数:
当您进入
callback()
时,传入的数据在ptr
上可用。不要忘记在退出
callback( )
时返回realsize
。You have to set up a callback function:
When you come into
callback( )
, the incoming data is available onptr
.Don't forget to return
realsize
on exit fromcallback( )
.