form libcurl in c
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 use开发者_如何学Gornames 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);
You have to set up a callback function:
...
curl_res = curl_easy_setopt( handle, CURLOPT_WRITEDATA, ptr );
curl_res = curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, callback );
...
When you come into callback( )
, the incoming data is available on ptr
.
Don't forget to return realsize
on exit from callback( )
.
精彩评论