cURL / Mailing Lists / curl-library / Single Mail

curl-library

Logging in from a POST method form

From: Donnell Connally <dconnall_at_uncc.edu>
Date: Mon, 25 Feb 2013 13:45:28 -0500

Hello all. I stumbled over libcURL as I was attempting to find me a
suitable replacement in C++ for Mechanize in Python as I much more enjoy
programming in the C languages than in Python (simply because C++ was my
first programming language.) I'm having a bit of trouble with the curl
post option as I'm trying to test login into my account on adf.ly.
Simply setting CURLOPT_URL to "http://adf.ly/login" and the rest of my
options works fine and returns the code of the page, but if I set
CURLOPT_POSTFIELDS, it returns a bad request (400.) If I set
CURLOPT_POSTFIELDS and then set CURLOPT_POST to 0 (so that it should
fill in the form but not submit it (correct me if I'm wrong),) it
returns the correct code of the page, but the form is not filled. The
fields are url encoded. This is my current code. What could be wrong?

string link = "http://adf.ly/login";
     string htmlCode;
     string url;
     string postData =
"dest%3Dpublisher%2Fapi%26email%3Ddistantminded%40gmail.com%26password%3Dprogram11";//+getEmailFromResources()+string("&password=")+getPasswordFromResources());

     CURL* c;

     curl_global_init(CURL_GLOBAL_ALL);

     c = curl_easy_init();
     curl_easy_setopt(c,CURLOPT_URL,link.c_str());
     curl_easy_setopt(c,CURLOPT_WRITEFUNCTION,MemCallback);
     curl_easy_setopt(c,CURLOPT_WRITEDATA,(void *)&htmlCode);
     curl_easy_setopt(c,CURLOPT_HEADER,1);
     curl_easy_setopt(c,CURLOPT_CONNECTTIMEOUT,5);
     curl_easy_setopt(c,CURLOPT_POSTFIELDS,postData.c_str());
     curl_easy_setopt(c,CURLOPT_POST,0);
     curl_easy_setopt(c,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1;
rv:15.0) Gecko/20100101 Firefox/13.0.1");

     if (curl_easy_perform(c) != 0)
         cout << "Curl Error";

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-02-25