libcurl cookies issue

Aug 1, 2013 at 6:46pm
I have a problem with my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

#include <curl.h>

static void print_cookies(CURL *curl) // funkcja otrzymuje uchwyt sesji
{

CURLcode res; // utworzenie zmiennej res
struct curl_slist *cookies=NULL; //utworzenie struktury
struct curl_slist *nc; //utworzenie drugiej struktury
int i; //pomocnicza zmienna

printf("Cookies, curl knows:\n"); //chwali sie ze otrzymał nastepujace cookies
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies); //wyciaga informacje o cookies i zrzuca ją do struktury
if (res != CURLE_OK) {//jesli ok
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));//jesli blad wysyła tą inforamcje
exit(1);//jesli blad konczy funkcje
}
nc = cookies, i = 1;
while (nc) {//dopoki nc nie jest 0 czyli dopoki cookies istnieje
printf("[%d]: %s\n", i, nc->data);
nc = nc->next;
i++;
}
if (i == 1) {
printf("(none)\n");
}
curl_slist_free_all(cookies);// koniec listy
}

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

curl_global_init(CURL_GLOBAL_ALL);//tworzy sesje
curl = curl_easy_init();
if (curl) {

curl_easy_setopt(curl, CURLOPT_URL, "http://google.com/");//ustala uchwyt sesji
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//zeby cookies pobierało
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
res = curl_easy_perform(curl);// wychwytuje błędy sesji
if (res != CURLE_OK) {
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
return 1;
}
else
fprintf(stderr, "prawidlowo rozpoczeto sesje");

print_cookies(curl);

printf("Erasing curl's knowledge of cookies!\n");
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
}

curl_global_cleanup();
return 0;
}


what happen? I get the error code: a libcurl function was given a bad argument
I'm begginer and I want to begin a biggest program. Sorry for mistake, I still learn Enlish language.
Last edited on Aug 1, 2013 at 6:47pm
Aug 2, 2013 at 1:23am
Not a solution, but FYI your code works fine on my Debian Sid system.
1
2
curl-config --version
libcurl 7.31.0


Changed the url to "www.google.com" and got:
1
2
3
4
5
6
Cookies, curl knows:
[1]: .google.com	TRUE	/	FALSE	1438478271	PREF	ID=0b457f4d880cea79:FF=0:TM=1375406271:LM=1375406271:S=VD0Bsrzx31VAxqIa
[2]: #HttpOnly_.google.com	TRUE	/	FALSE	1391217471	NID	67=KQbuNV81t8gaTusSJhudu57IFK84PbV7OwIA74aFhhHW_yx6DUYwzLaLN_0iherAyRq9d-zkX33xfjcE2G3r4T21Z7amoV11OVpHU_BTwoFYH1K-2NRxgjzhriVq_a-x
Erasing curl's knowledge of cookies!
* Connection #0 to host www.google.com left intact
prawidlowo rozpoczeto sesje 
Aug 2, 2013 at 9:59am
It's entire code, that you have in console? i get another.
* About to connect() to www.google.com port 80
* Trying 173.194.113.144... * connected
* Connected to www.google.com (173.194.113.144) port 80
> GET / HTTP/1.1
Host: www.google.com
Accept: */*

< HTTP/1.1 302 Found
< Location: http://www.google.pl/?gws_rd=cr
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
* Added cookie PREF="ID=380d3b08e23eeebc:FF=0:TM=1375437202:LM=1375437202:S=ex0x
XBbVjaBIr1Uk" for domain google.com, path /, expire 1438512802
< Set-Cookie: PREF=ID=380d3b08e23eeebc:FF=0:TM=1375437202:LM=1375437202:S=ex0xXB
bVjaBIr1Uk; expires=Sun, 02-Aug-2015 09:53:22 GMT; path=/; domain=.google.com
* Added cookie NID="67=lAWE3VCmydWgBsR3Mp7GYVQkv2kVUAPEbyVeoxwNV8RzyfP0kvRHEgeSx
9AosaKqdIKKjcnV2T_3utNB0wlr-uz-_iPOUYp1-JVtjhANtlzXm1OrYYz2_w-xp_bA9ZFc" for dom
ain google.com, path /, expire 1391248402
< Set-Cookie: NID=67=lAWE3VCmydWgBsR3Mp7GYVQkv2kVUAPEbyVeoxwNV8RzyfP0kvRHEgeSx9A
osaKqdIKKjcnV2T_3utNB0wlr-uz-_iPOUYp1-JVtjhANtlzXm1OrYYz2_w-xp_bA9ZFc; expires=S
at, 01-Feb-2014 09:53:22 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/
bin/answer.py?hl=en&answer=151657 for more info."
< Date: Fri, 02 Aug 2013 09:53:22 GMT
< Server: gws
< Content-Length: 228
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.pl/?gws_rd=cr">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact
prawidlowo rozpoczeto sesjeCookies, curl knows:
Curl curl_easy_getinfo failed: a libcurl function was given a bad argument
Aug 2, 2013 at 2:29pm
You are receiving a redirect. Maybe setting the "CURLOPT_FOLLOWLOCATION" option to true will help.

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTFOLLOWLOCATION
Aug 2, 2013 at 8:43pm
I tried it. Doesn't help me,

You claim that program work valid? please give me program name, which you use? which OS is it?
Last edited on Aug 2, 2013 at 8:47pm
Aug 2, 2013 at 8:57pm
Can you give me link with this system and program, which you use to compiling
Aug 3, 2013 at 1:22am
I compiled the exact code that you posted above, with the exception of the addition of the "CURLOPT_FOLLOWLOCATION" line on the following OS's and it works fine.

$ lsb_release -d
Description: Debian GNU/Linux 7.1 (wheezy)

$ curl-config --version
libcurl 7.26.0

$ g++ -v
. . .
gcc version 4.7.2 (Debian 4.7.2-5)


Debian GNU/Linux unstable (sid)
libcurl 7.31.0
gcc version 4.7.3 (Debian 4.7.3-6)


FreeBsd 9.1 RELEASE
libcurl 7.24.0
gcc version 4.2.1


Can you give me link with this system and program, which you use to compiling

http://www.debian.org/distrib/
Topic archived. No new replies allowed.