2011-05-21 69 views
4

所以我试图使用Network.Browser库来存储cookie会话。这是我到目前为止有:使用Network.Browser创建cookie

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 

此给出以下的输出:

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=Uyc3NS4zNi4yMTMuMjgnCnAxCi4=", 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=/ 

最后一行是PutStrLn的结果,我可以运行经过秒差距,如果我真的需要..但在此之上,从浏览输出中,有一个MkCookie构造函数,它具有我需要的所有信息。有没有办法很容易地获得呢?

回答

1

getCookies里面的BrowserAction monad返回接受的cookie列表。像这样使用它:

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