cURL / Mailing Lists / curl-library / Single Mail

curl-library

libcurl loop

From: Tyler Brock <tyler.brock_at_gmail.com>
Date: Tue, 21 Sep 2010 22:10:20 -0400

Hey guys,

I'm trying to use curl to login and retrieve information from a couple of
web pages in a loop using a two step process.

The first curl logs in to the page.
The second curl takes the same curl handle and gets another page which is
stored in memory to later be parsed by another function that I wrote.

The problem I'm having is that individually each run of the loop works when
isolated, but when you do two or more in a row it fails to
login successfully.

I've pasted the relevant code below, any help would be greatly appreciated.
Thanks!

-Tyler

static void run_custodian(ClientStruct *cs, int num_clients)
{
  int i;
  for(i = 0; i < num_clients; i++)
     {
       char current_url[MAX_URL_SIZE];
       char buffer[CURL_ERROR_SIZE];
       CURL *curl_handle;
       MemoryStruct data;

       curl_global_init(CURL_GLOBAL_ALL);

       init_ms(&data);

       curl_handle = init_curl_handle(curl_handle, buffer);
       curl_handle = init_mssb_specific(curl_handle, cs[i]);

       puts("logging in");
       build_mssb_login(current_url);
       curl_custodian(curl_handle, buffer, &data, current_url);

       puts("getting holdings");
       build_mssb_url(current_url);
       curl_custodian(curl_handle, buffer, &data, current_url);
       print_ms_mssb(&data, cs[i]);

       if(data.memory) free(data.memory);
       curl_easy_cleanup(curl_handle);
     }
}

static CURL * init_curl_handle(CURL *ch, char *buffer)
{
  //puts("initializing handle...");
  if((ch = curl_easy_init()) != NULL)
    {
      curl_easy_setopt(ch, CURLOPT_POST, 1);
      curl_easy_setopt(ch, CURLOPT_USERAGENT, IE_USER_AGENT);
      curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, buffer);
      curl_easy_setopt(ch, CURLOPT_COOKIEFILE, ""); //starts cookie engine
    }
  return ch;
}

static void init_ms(MemoryStruct *ms)
{
  ms->memory = NULL;
  ms->size = 0;
}

static void curl_custodian(CURL *ch, char *buffer, MemoryStruct *ms, char
*current_url)
{
  set_curl_url(ch, current_url);
  set_ms(ch, ms);
  if (curl_easy_perform(ch) != CURLE_OK) {
    fprintf(stderr, "%s\n", buffer);
    exit(1);
  }
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-09-22