使用Dispatch在Scala中为HTTP POST设置cookie

时间:2013-10-24 20:22:14

标签: scala post cookies scala-dispatch

我似乎无法使用Dispatch设置cookie。服务器重新发送一个新的会话ID,暗示我尝试发送的会话ID没有以正确的方式发送。这是代码:

val domain = "myhost.com"
val host_req = host(domain).secure
val request = host_req / "path" / "path"

def post = request << Map("key" -> "SomeValue")

val response: Either[Throwable, Map[String, String]] =
    Http(post OK asHeaders).either()

//The cookie comes down in the form "Set-Cookie: SESSIONID=<somesession>; Path=/path/; HttpOnly"

//successfully retrieves the session id...
val sessionId = getSessionId(response)
println(sessionId)
val sessionCookie = new com.ning.http.client.Cookie(domain, "SESSIONID", sessionId, "/path/", -1, true)

request.POST.addCookie(sessionCookie)
def establishPost = request << Map("key" -> "SomeValue")
establishPost.addCookie(sessionCookie)

val establishResponse: Either[Throwable, Map[String, String]] =
    Http(establishPost OK asHeaders).either()


//Server sends down new SESSIONID...
//sessionId != getSEssionId(establishPost)

这是使用最新版本的Dispatch。我正在尝试学习Scala,我唯一无法弄清楚如何做的是在它作为请求发送之前检查其标题的establishPost对象。

1 个答案:

答案 0 :(得分:2)

这应该更好:

def reqWithParams = request << Map("key" -> "SomeValue")
val reqWithCookies = reqWithParams.addCookie(sessionCookie)

addCookie方法返回新对象(带有cookie的对象),但是您的代码没有使用它。