GET URL with cookies

Trevor DeVore lists at mangomultimedia.com
Thu Jul 10 10:46:57 EDT 2008


On Jul 10, 2008, at 10:10 AM, Bert Shuler wrote:

> I have a site which I need to authenticate to. I do this:
>
> put URL "http://site.com/login?uid="&URLEncode("bert at site.com") &  
> "&password=" & URLEncode("mypass") into field "html"
>
> The host site validates my password, sets a cookie, and redirects me  
> to the front page, which because I have a validated cookie, shows me  
> I am logged in. This works fine in a browser, but it seem "put URL"  
> is doing the redirect, but dropping the cookie. So I never actually  
> get logged in.
>
> My question is this, can I make "put URL" keep the cookies? Or, as  
> an alternate solution, can I make the revBrowser upload files with  
> zero user intervention?
>
> Any pointers will be appreciated.

Hi Bert,

I think you have to handle cookies manually. I just did some work with  
cookies yesterday so here are some notes based on that experience.

I'm using 'post' rather than 'put' but after executing the 'post'  
command I used libURLLastRHHeaders() to retrieve the header that the  
server sent back. You will find at least one line that starts with  
'Set-Cookie'.

What you are responsible for doing is extracting the cookie from that  
line and creating a 'Cookie' entry in the header you send to the  
server with each request. I put together a handler that takes  
libURLLastRHHeaders(), extracts the cookies and stores them in a  
script local variable (this code is for a library, you could easily  
return the result) that I then assign to 'the httpheaders' property  
before I send a request to the server expecting the cookie. Here is  
what it looks like:

##===================

local sCookieHeader


## Parse header returned from a server and create a cookie header that  
can be sent back: Cookie: cookie1;cookie2;cookie3;...
private command  _StoreCookies pHeader
     local theCharNo,theCookieLine,theLineNo,theOffset

     put empty into sCookieHeader
     put 0 into theOffset
     repeat forever
         put lineoffset("Set-Cookie:", pHeader, theOffset) into  
theLineNo
         if theLineNo > 0 then
             add theOffset to theLineNo
             put line theLineNo of pHeader into theCookieLine
             delete word 1 of theCookieLine ## Set-Cookie:
             put offset(";", theCookieLine) into theCharNo
             if theCharNo > 0 then
                 delete char theCharNo to -1 of theCookieLine
             end if
             put theCookieLine & ";" after sCookieHeader
             put theLineNo into theOffset
         else
             exit repeat
         end if
     end repeat

     if the last char of sCookieHeader is ";" then
         delete the last char of sCookieHeader
     end if

     if sCookieHeader is not empty then
         put "Cookie: " before sCookieHeader
     end if

     return empty
end _StoreCookies

##======================

Hopefully this helps.

-- 
Trevor DeVore
Blue Mango Learning Systems
ScreenSteps: http://www.screensteps.com
Developer Resources: http://revolution.bluemangolearning.com



More information about the use-livecode mailing list