如何使用Scala分派获取301重定向中返回的URL? [英] How do I use Scala dispatch to get the URL returned in a 301 redirect?
问题描述
我正在使用Scala dispatch HTTP库,版本0.10.1.我向返回HTTP 301(永久重定向)的URL发出请求.例如, http://wikipedia.com 返回301,该重定向到
I am using Scala dispatch HTTP library, version 0.10.1. I make a request to a URL that returns an HTTP 301, permanent redirect. For example, http://wikipedia.com returns a 301 that redirects to http://www.wikipedia.org/. How do I do I use dispatch to get the redirected URL?
在学习本教程之后,这就是我所做的.
Following the tutorial, here's what I've done.
import dispatch._, Defaults._
val svc = url("http://wikipedia.com")
val r = Http(svc OK as.String)
r()
这将引发意外的响应状态:301"异常.大概我需要查询重定向URL的r
值,或者在其定义中指定OK
以外的其他参数,但是我无法从文档中找出要做什么.
This throws a "Unexpected response status: 301" exception. Presumably I need to either query the r
value for the redirected URL, or maybe specify some argument other than OK
in its definition, but I can't figure out what to do from the documentation.
推荐答案
配置基础asyncClient进行重定向:
Configure the underlying asyncClient to follow redirects:
val r = Http.configure(_ setFollowRedirects true)(svc OK as.String)
要获取重定向的URL,请执行以下操作:
To get the redirected URL:
val svc = url("http://wikipedia.com/")
val r = Http(svc > (x => x))
val res = r()
println(res.getHeader("Location"))
这篇关于如何使用Scala分派获取301重定向中返回的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!