Erlang http 客户端库 httpc 的例子

1 > inets:start().
ok
2> http:set_options([{cookies, enabled}]).
ok
3>{ok, {{NewVersion, 200, NewReasonPhrase}, NewHeaders, NewBody}} =
httpc:request(get, {“http://www.google.com”, [{“connection”, “close”}]},
[], []).

httpc:reset_cookies().
httpc:store_cookies([{“set-cookie”,”TESTID=set”}], URL). %% 注意这里 URL 的格式是 http://www.a.com/aaa ,httpc 将使用 www.a.com /aaa 。
httpc:which_cookies().

erlang 的 cookie 在重定向时会多发一次cookie 下面是一个例子:
第一次请求的时候没有问题:

GET / HTTP/1.1
te:
host: www.google.com
connection: close

HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1305091134484018&usg=AFQjCNGVKwFoguLJsXcF2r-iSVP8pGVjAg
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=9837e8b8b27a4644:FF=0:NW=1:TM=1305091104:LM=1305091104:S=6c4IRHZYSSNbdUuu; expires=Fri, 10-May-2013 05:18:24 GMT; path=/; domain=.google.com
Date: Wed, 11 May 2011 05:18:24 GMT
Server: gws
Content-Length: 376
X-XSS-Protection: 1; mode=block
Connection: close


GET /url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1305091134484018&usg=AFQjCNGVKwFoguLJsXcF2r-iSVP8pGVjAg HTTP/1.1
te:
host: www.google.com.hk
connection: close

HTTP/1.1 302 Found
Location: http://www.google.com.hk/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=9e289483a992f2d3:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091104:S=yZ76ERBlUrruTyf8; expires=Fri, 10-May-2013 05:18:24 GMT; path=/; domain=.google.com.hk
Date: Wed, 11 May 2011 05:18:24 GMT
Server: gws
Content-Length: 222
X-XSS-Protection: 1; mode=block
Connection: close


GET / HTTP/1.1
te:
host: www.google.com.hk
connection: close
cookie: $Version=0; PREF=ID=9e289483a992f2d3:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091104:S=yZ76ERBlUrruTyf8; $Path=/; $Domain=.google.com.hk

HTTP/1.1 200 OK
Date: Wed, 11 May 2011 05:18:24 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=GB2312
Set-Cookie: NID=46=cgV8OXUUqoZuZBzyUYB3hZC1sL51UZhsZ8pG6g_tydOhFzj1XIRCjSUTSPge97fbOmafxdA6GHZh1eKokS7Q5sj9JHgi7SmYtW3oPZIE_Sy6FYt8dQxUJ3hstV-RRNrv; expires=Thu, 10-Nov-2011 05:18:24 GMT; path=/; domain=.google.com.hk; HttpOnly
Server: gws
X-XSS-Protection: 1; mode=block
Connection: close

第二次就会出现问题了

GET / HTTP/1.1
te:
host: www.google.com
connection: close
cookie: $Version=0; PREF=ID=9837e8b8b27a4644:FF=0:NW=1:TM=1305091104:LM=1305091104:S=6c4IRHZYSSNbdUuu; $Path=/; $Domain=.google.com

HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1305091219434173&usg=AFQjCNGrXvfQAy3xZMSZz2k2q4slxaeW4Q
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Date: Wed, 11 May 2011 05:19:49 GMT
Server: gws
Content-Length: 376
X-XSS-Protection: 1; mode=block
Connection: close


GET /url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1305091219434173&usg=AFQjCNGrXvfQAy3xZMSZz2k2q4slxaeW4Q HTTP/1.1
te:
host: www.google.com.hk
connection: close
cookie: $Version=0; PREF=ID=9837e8b8b27a4644:FF=0:NW=1:TM=1305091104:LM=1305091104:S=6c4IRHZYSSNbdUuu; $Path=/; $Domain=.google.com
cookie: $Version=0; PREF=ID=9e289483a992f2d3:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091104:S=yZ76ERBlUrruTyf8; $Path=/; $Domain=.google.com.hk; NID=46=cgV8OXUUqoZuZBzyUYB3hZC1sL51UZhsZ8pG6g_tydOhFzj1XIRCjSUTSPge97fbOmafxdA6GHZh1eKokS7Q5sj9JHgi7SmYtW3oPZIE_Sy6FYt8dQxUJ3hstV-RRNrv; $Path=/; $Domain=.google.com.hk

HTTP/1.1 302 Found
Location: http://www.google.com.hk/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=9837e8b8b27a4644:U=ab1c598a16c1d65a:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091189:S=oioGImT3t2mz6D-J; expires=Fri, 10-May-2013 05:19:49 GMT; path=/; domain=.google.com.hk
Date: Wed, 11 May 2011 05:19:49 GMT
Server: gws
Content-Length: 222
X-XSS-Protection: 1; mode=block
Connection: close


GET / HTTP/1.1
te:
host: www.google.com.hk
connection: close
cookie: $Version=0; PREF=ID=9837e8b8b27a4644:FF=0:NW=1:TM=1305091104:LM=1305091104:S=6c4IRHZYSSNbdUuu; $Path=/; $Domain=.google.com
cookie: $Version=0; PREF=ID=9e289483a992f2d3:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091104:S=yZ76ERBlUrruTyf8; $Path=/; $Domain=.google.com.hk; NID=46=cgV8OXUUqoZuZBzyUYB3hZC1sL51UZhsZ8pG6g_tydOhFzj1XIRCjSUTSPge97fbOmafxdA6GHZh1eKokS7Q5sj9JHgi7SmYtW3oPZIE_Sy6FYt8dQxUJ3hstV-RRNrv; $Path=/; $Domain=.google.com.hk
cookie: $Version=0; NID=46=cgV8OXUUqoZuZBzyUYB3hZC1sL51UZhsZ8pG6g_tydOhFzj1XIRCjSUTSPge97fbOmafxdA6GHZh1eKokS7Q5sj9JHgi7SmYtW3oPZIE_Sy6FYt8dQxUJ3hstV-RRNrv; $Path=/; $Domain=.google.com.hk; PREF=ID=9837e8b8b27a4644:U=ab1c598a16c1d65a:FF=2:LD=zh-CN:NW=1:TM=1305091104:LM=1305091189:S=oioGImT3t2mz6D-J; $Path=/; $Domain=.google.com.hk

HTTP/1.1 200 OK
Date: Wed, 11 May 2011 05:19:49 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Set-Cookie: PREF=ID=9837e8b8b27a4644:U=ab1c598a16c1d65a:FF=0:NW=1:TM=1305091104:LM=1305091189:S=8wPoAJMKqBOdg6M_; expires=Fri, 10-May-2013 05:19:49 GMT; path=/; domain=.google.com.hk
Server: gws
X-XSS-Protection: 1; mode=block
Connection: close

发表评论

电子邮件地址不会被公开。