1

我似乎无法使用 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,唯一我不知道该怎么做的就是在作为请求发送之前检查它的标头的建立邮政对象。

4

1 回答 1

3

这应该更好:

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

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

于 2013-10-24T23:36:24.060 回答