Play Framework 2.1:Scala:如何获取整个基本URL(包括协议)? [英] Play Framework 2.1: Scala: how to get the whole base url (including protocol)?

查看:57
本文介绍了Play Framework 2.1:Scala:如何获取整个基本URL(包括协议)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我可以从请求,其中包括域和可选端口.不幸的是,它不包含协议(http vs https),因此我无法为网站本身创建绝对URL.

Currently I am able to get the host from the request, which includes domain and optional port. Unfortunately, it does not include the protocol (http vs https), so I cannot create absolute urls to the site itself.

object Application extends Controller {
  def index = Action { request =>
    Ok(request.host + "/some/path") // Returns "localhost:9000/some/path"
  }
}

有什么方法可以从请求对象中获取协议吗?

Is there any way to get the protocol from the request object?

推荐答案

实际上,您的端口号是http还是https.

Actually your portnumber will give you if it's http or https.

通过https支持JAVA_OPTS=-Dhttps.port=9001 play start

这是一个代码段(您可以使用正则表达式使验证更加稳定,从属性中获取https端口号...)

Here's a code snippet (you can make the validation more stable with a regex, take the https port number from properties ...)

def path = Action { request =>
    val path = 
      if(request.host.contains(":9000"))
        "http://" + request.host + "/some/path"
      else
        "https://" + request.host + "/some/path"
    Ok(path)
  }

代码将返回

http://ServerIp:9000/some/path if it's thru http
https://ServerIp:9001/some/path if it's thru https

这篇关于Play Framework 2.1:Scala:如何获取整个基本URL(包括协议)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆