使用Network.Browser创建cookie

时间:2011-05-21 07:03:58

标签: haskell

所以我正在尝试使用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的结果,如果我真的需要,我可以通过parsec运行..但是在上面,从浏览器的输出,有一个MkCookie构造函数,包含我需要的所有信息。有什么方法可以轻易获得吗?

1 个答案:

答案 0 :(得分:1)

getCookies monad中的

BrowserAction会返回已接受的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