我想要result.entity.asString的内容,但是结果是空的。我有很多警告,让你不停下来。我的代码是:`
implicit val system: ActorSystem = ActorSystem("simple-spray-client")
val log: LoggingAdapter = Logging(system, getClass)
val pipeline: SendReceive = sendReceive
val responseFuture: Future[HttpResponse] = pipeline {
Get("https://api.guildwars2.com/v2/items")
}
val re = responseFuture.onComplete {
case Success(result: HttpResponse) => {
result.entity.asString
shutdown()
}
case Failure(error) =>
log.error(error, "Couldn't get list of items")
shutdown()
}
def shutdown(): Unit = {
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
}}`结果是:
()
[WARN] [10/27/2015 11:26:04.776] [simple-spray-client-akka.actor.default-dispatcher-4] [akka://simple-spray-client/user/IO-HTTP/group-0/0] Illegal response header: Illegal 'Cache-Control' header: Invalid input '"', expected $timesCache$minusControl (line 1, pos 1):
"public, max-age=300"
^
[WARN] [10/27/2015 11:26:04.779] [simple-spray-client-akka.actor.default- dispatcher-4] [akka://simple-spray-client/user/IO-HTTP/group-0/0] Illegal response header: Illegal 'Access-Control-Expose-Headers' header: Invalid input '"', expected $timesAccess$minusControl$minusExpose$minusHeaders (line 1, pos 1):
"X-Result-Total, X-Result-Count"
^发布于 2015-10-27 19:53:57
如何开始一些代码清理和格式化?:) (responseFuture.toString?println(re) ?)这肯定会有助于吸引答案。
这就是说,请求是由spray触发的。警告是关于响应中的标头的。我运行了查询,响应头是:
Cache-Control: "public, max-age=300"
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Content-Language: en
Expires: Tue, 27 Oct 2015 11:44:47 +0000
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Result-Total: 48798
X-Result-Count: 48798
Access-Control-Expose-Headers: "X-Result-Total, X-Result-Count"
Access-Control-Allow-Origin: *
X-Content-Type-Options: nosniff
Date: Tue, 27 Oct 2015 11:39:47 GMT 正如您所看到的,Cache-Control和Access-Control-Expose-Headers的值以双引号开头,正如spray所警告的那样。
现在,您可以尝试重新实现为
pipeline {
Get("https://api.guildwars2.com/v2/items")
}.onComplete {
case Success(result: HttpResponse) =>
log.info("Result: "+result.entity.asString)
shutdown()
case Failure(error) =>
log.error(error, "Couldn't get list of items")
shutdown()
}...and查看控制台上info级别记录的内容(不确定您使用的是什么日志记录框架)
此外,在访问实体之前检查HTTP响应代码可能是一个好主意
发布于 2015-10-27 19:56:04
我跟踪您共享的日志消息,并向api url发出get请求。这里是响应头;
您可以看到Cache-Control和Access-Control-Expose-Headers具有双引号"..",这是日志消息所指示的。
我认为你应该首先查看http status code,这有助于发现主要问题。

发布于 2016-03-10 23:23:59
据我所知你想要:
import scala.concurrent.Await
import scala.concurrent.duration._
def extract(responseFuture: Future[HttpResponse]): HttpResponse = Await.result(responseFuture, xx.seconds)和
val myOlolo = extract(responseFuture).entity.asStringhttps://stackoverflow.com/questions/33365625
复制相似问题