1

My goal is to retreive the cookies from a webBrowser Control. I tried to do this with a reference, but Event.map doesn't allow me to return the value:

let getCookie(url:string) = 
    let form  = new Form(Text="Internet Navigator")
    form.AutoScaleDimensions <- new System.Drawing.SizeF(6.0F, 13.0F)
    form.AutoScaleMode <- System.Windows.Forms.AutoScaleMode.Font
    form.ClientSize <- new System.Drawing.Size(849, 593)
    form.ResumeLayout(false)
    form.PerformLayout()

    let wb = new WebBrowser()
    wb.Visible<-true
    wb.AutoSize<-true
    wb.Size <- new System.Drawing.Size(804, 800)
    form.Controls.Add(wb)
    form.Show()


    let cookie = ref ""

    wb.Navigate(url)
    wb.DocumentCompleted
    |> Event.map(fun _ -> cookie:= wb.Document.Cookie)
    !cookie

Ideally there is a way to return the cookie value from within the Event.map (or something like that)?

Gabe
  • 84,912
  • 12
  • 139
  • 238
jlezard
  • 1,417
  • 2
  • 15
  • 32

1 Answers1

3

UPDATED : To return cookie value from the function rather than using callback

let getCookie(url:string) = 
        let form  = new Form(Text="Internet Navigator")
        form.AutoScaleDimensions <- new System.Drawing.SizeF(6.0F, 13.0F)
        form.AutoScaleMode <- System.Windows.Forms.AutoScaleMode.Font
        form.ClientSize <- new System.Drawing.Size(849, 593)
        form.ResumeLayout(false)
        form.PerformLayout()

        let wb = new WebBrowser()
        wb.Visible<-true
        wb.AutoSize<-true
        wb.Size <- new System.Drawing.Size(804, 800)
        form.Controls.Add(wb)
        wb.DocumentCompleted |> Event.add (fun _ -> form.Close())
        wb.Navigate(url)
        form.ShowDialog() |> ignore
        wb.Document.Cookie

[<STAThreadAttribute>]
do
    let cookie = getCookie "http://www.google.com"
    Console.Read() |> ignore
Ankur
  • 33,367
  • 2
  • 46
  • 72
  • thanks for your reply, the print works perfect, but it doesn't help solve the issue of trying to get the value of the cookie once the Document has loaded, in my example which is same as let temp= ref " " , let f res = temp:=res, the code runs really fast so the ref is always "", maybe a little later temp will be the correct cookie value – jlezard Sep 13 '11 at 13:43
  • Updated the answer to return cookie value from the function – Ankur Sep 14 '11 at 04:45