如何设置cookie?
How to set cookie?
我有以下代码,它使用 http://fsharp.github.io/FSharp.Data/library/Http.html 来登录网站并获取一些文件。但是,每个页面都有一段 jQuery 脚本来设置 cookie - $.cookie("Authenticated", 14313432);
。我的代码也必须模仿 jQuery 脚本来设置 cc
。否则认证将失败。如何使用 F# 数据:HTTP 实用程序?
let cc = CookieContainer()
let h = Http.RequestString(url, httpMethod = "GET", cookieContainer = cc, headers = headers) // embedded jQuery will set the cookie
let body = HttpRequestBody.FormValues ["UserName", "xxx"; "Password", "ppp"]
Http.RequestString(url, body=body, httpMethod="POST", cookieContainer=cc, headers = ...
let page = Http.RequestString(....) // embedded jQuery script will set the cookie with different value
let page2 = Http.RequestString(....) // embedded jQuery script will set the cookie with different value
如果我没猜错,你不知道如何将 "Authenticated", 14313432
作为 cookie 插入,但你知道如何使用 CookieContainer
对吗?
那么 CookieContainer
is just a .net class contiaining Cookie
对象,所以这应该可以解决问题:
let cc = CookieContainer()
let cookie = Cookie ("Authenticated", "14313432")
cc.Add cookie
// the rest of your code
我有以下代码,它使用 http://fsharp.github.io/FSharp.Data/library/Http.html 来登录网站并获取一些文件。但是,每个页面都有一段 jQuery 脚本来设置 cookie - $.cookie("Authenticated", 14313432);
。我的代码也必须模仿 jQuery 脚本来设置 cc
。否则认证将失败。如何使用 F# 数据:HTTP 实用程序?
let cc = CookieContainer()
let h = Http.RequestString(url, httpMethod = "GET", cookieContainer = cc, headers = headers) // embedded jQuery will set the cookie
let body = HttpRequestBody.FormValues ["UserName", "xxx"; "Password", "ppp"]
Http.RequestString(url, body=body, httpMethod="POST", cookieContainer=cc, headers = ...
let page = Http.RequestString(....) // embedded jQuery script will set the cookie with different value
let page2 = Http.RequestString(....) // embedded jQuery script will set the cookie with different value
如果我没猜错,你不知道如何将 "Authenticated", 14313432
作为 cookie 插入,但你知道如何使用 CookieContainer
对吗?
那么 CookieContainer
is just a .net class contiaining Cookie
对象,所以这应该可以解决问题:
let cc = CookieContainer()
let cookie = Cookie ("Authenticated", "14313432")
cc.Add cookie
// the rest of your code