Ene*_*nso 13 scala request playframework-2.1
目前,我能够从请求中获取主机,其中包括域和可选端口.不幸的是,它不包括协议(http vs https),因此我无法为网站本身创建绝对网址.
object Application extends Controller {
def index = Action { request =>
Ok(request.host + "/some/path") // Returns "localhost:9000/some/path"
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从请求对象中获取协议?
小智 10
实际上有一种简单的方法可以使用反向路由器用来实现类似功能的Call类.鉴于您属于隐式请求的范围,您可以执行以下操作:
new Call(request.method, input.request).absoluteURL()
Run Code Online (Sandbox Code Playgroud)
它将为您提供完整的URL(协议,主机,路由和参数).
小智 2
实际上,如果是 http 或 https,您的端口号将会告诉您。
使用 https 支持启动您的 Play 服务器JAVA_OPTS=-Dhttps.port=9001 play start
这是一个代码片段(您可以使用正则表达式使验证更加稳定,从属性中获取 https 端口号...)
def path = Action { request =>
val path =
if(request.host.contains(":9000"))
"http://" + request.host + "/some/path"
else
"https://" + request.host + "/some/path"
Ok(path)
}
Run Code Online (Sandbox Code Playgroud)
代码将返回
http://ServerIp:9000/some/path if it's thru http
https://ServerIp:9001/some/path if it's thru https
Run Code Online (Sandbox Code Playgroud)