开发者

Creating cookies with Network.Browser

So I'm trying to use the Network.Browser library to store cookie sessions. Here's what I have so far:

main = do
   args <- getArgs
   [user, pass] <- if not (null args) then return args else do
     user <- fetch "username"
     pass <- fetch "password"
     return [user, pass]
    sup <- browse $ do
 --       setOutHandler $ const $ return ()
        (request (getRequest ("http://www.plurk.com/API/Users/login?"
                            ++ "username="  ++ user
                            ++ "&password=" ++ pass
                            ++ "&api_key=FVXhr06sHbbp7Lv2rezPVw8rhpl7fNnX")))
   let (_,a) = rspHeaders <$> sup
 --  let (Right b) = parseJSON a
 --  let (Just c) = findIn b ["fans_count"]
   putStrLn $ hdrValue (a!!5)

 fetch info = do
   putStr $ "Enter your plurk " ++ info ++ ": "
   hFlush stdout
   return =<< getLine

this gives the following output:

Sending:
GET /API/Users/login?username=saiko_chriskun&password=XXXXXXXXXX&api_key=FVXhr06sHbbp7Lv2rezPVw8rhpl7fNnX HTTP/1.1
Host: www.plurk.com
Content-Length: 0
User-Agent: hs-HTTP-4000.0.9


Creating new connection to www.plurk.com
Received:
HTTP/1.1 200 OK 
Server: nginx/1.0.0
Date: Sat, 21 May 2011 06:37:41 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Set-Cookie: plurkcookiea="bYQstPmQg58L5hTs9nZ7fvIEzuc=?age=STIwCi4=&api_login=STAxCi4=&chkN=Uyc4YzU1ZWM3YTMzNzQxM2I2MDM2MmY1ZWZhODM5NzdhOScKcDEKLg==&gender=TDFMCi4=&user_id=TDgwNTc3NDlMCi4=&user_ip=Uyc3NS4zNi4yMTMuMjgnCnAxCi4="; Domain=.plurk.com; expires=Sat, 04-Jun-2011 06:37:41 GMT; Max-Age=1209600; Path=/
Expires: Sat, 21 May 2011 06:37:40 GMT
Cache-Control: no-cache
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 2648


Cookies received:
  MkCookie {ckDomain = ".plurk.com", ckName = "plurkcookiea", ckValue = "bYQstPmQg58L5hTs9nZ7fvIEzuc=?age=STIwCi4=&api_login=STAxCi4=&chkN=Uyc4YzU1ZWM3YTMzNzQxM2I2MDM2MmY1ZWZhODM5NzdhOScKcDEKLg==&gender=TDFMCi4=&user_id=TDgwNTc3NDlMCi4=&user_ip=U开发者_JAVA技巧yc3NS4zNi4yMTMuMjgnCnAxCi4=", ckPath = Just "/", ckComment = Nothing, ckVersion = Nothing}
Accepting cookies with names: plurkcookiea
plurkcookiea="bYQstPmQg58L5hTs9nZ7fvIEzuc=?age=STIwCi4=&api_login=STAxCi4=&chkN=Uyc4YzU1ZWM3YTMzNzQxM2I2MDM2MmY1ZWZhODM5NzdhOScKcDEKLg==&gender=TDFMCi4=&user_id=TDgwNTc3NDlMCi4=&user_ip=Uyc3NS4zNi4yMTMuMjgnCnAxCi4="; Domain=.plurk.com; expires=Sat, 04-Jun-2011 06:37:41 GMT; Max-Age=1209600; Path=/

The last line is the result of PutStrLn, and I could run that through parsec if I really needed to.. but above that, from the output of browse, there's a MkCookie constructor with all the info I need. Is there someway to easily obtain that?


getCookies inside a BrowserAction monad returns the list of accepted cookies. Use it like this:

import Network.HTTP
import Network.Browser

main = do
    (rsp, cookies) <- browse $ do
        rsp <- request $ getRequest "http://google.com/"
        cookies <- getCookies
        return (rsp, cookies)
    print cookies
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜