Version 3 of Cookies

Updated 2003-04-15 13:27:18

A lot of webservers use client-side cookies for logging in. Here is some sample code of how to log in to an OpenACS 3.x server:


 package require http
 set login [::http::formatQuery email [email protected] password fooFoo!]

 set tok [::http::geturl -query $login]
 upvar \#0 $tok state
 set cookies [list]
 foreach {name value} $state(meta) {
     if { $name eq "Set-Cookie" } {
         lappend cookies [lindex [split $value {;}] 0]
 ::http::cleanup $tok

Using it

 set tok2 [::http::geturl -headers [list Cookie [join $cookies {;}]]]
 ... your code
 ::http::cleanup $tok2

This method will work for a lot of websites that ask for a username and password. For each site, you'll probably want to read the page source of the login page of that site to see where it POSTs to and what fields you need to supply. And you may need to play with your User-Agent headers too. A lot of sites actually restrict which browsers can login...

This page also serves as a reference for all pages that deal with Cookie management by CGI processing scripts.

Also see Cookies: give and take