使用 scalaj http 库时如何提取 SSO cookie

How to extract SSO cookie when using scalaj http library

我有 HTTP 回复。我正在尝试使用 scala 从中获取 header 之一的值。 我的代码类似于以下内容:

import scalaj.http.Http

val result = Http("http:///sample.com")
  .postData("""{"Username":"user1","password":"pass"""")
  .header("Content-Type", "application")
  .header("Accept", "text/plain")
val headers = result.headers.mkString

println("Headers: " + headers)

header 看起来类似于以下内容:

Cache-Control -> Vector(no-Store)
Content-Type -> Vector(text/html;charset=ISO-8859-l)
Set-Cookie -> Vector(SESSIONID=D122334;path=/a/b/c;SSO=000112233445)

从这个 header 中,我想单独提取 SSO 值。使用下面的代码,我可以正确打印它们。

for((k,v) <- result.headers) println(s"key: $k\nvalue: $v\n")

得到以下结果:

key: Cache-Control
value: Vector(no-Store)

key: Content-Type
value: Vector(text/html;charset=ISO-8859-l)

key: Set-Cookie
value: Vector(SESSIONID=D122334;path=/a/b/c;SSO=000112233445)

我想单独提取键 Set-CookieSSO 的数据。 我知道如何使用 python

实现此目的

我是 Scala 的新手。有人可以帮我解决这个问题吗?

使用 cookies 方法并按名称过滤 cookie

  val ssoToken = Http("http:///sample.com")
    .postData("""???""")
    .header("Content-Type", "application")
    .header("Accept", "text/plain")
    .asBytes
    .cookies
    .find(_.getName == "SSO")
    .map(_.getValue)

找到token后会得到Some("tokenValue"),如果想得到String,可以按下面的方法

ssoToken.getOrElse(sys.error("SSO Token not found"))

找不到token会抛出异常