在 Servlet 中获取 HTTP 和 HTTPS 请求的完整 URL 和查询字符串 [英] Get full URL and query string in Servlet for both HTTP and HTTPS requests

查看:27
本文介绍了在 Servlet 中获取 HTTP 和 HTTPS 请求的完整 URL 和查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,其任务是检索请求的 URL 或完整路径.我写了这段代码:

I am writing a code which task is to retrieve a requested URL or full path. I've written this code:

HttpServletRequest request;//obtained from other functions
String uri = request.getRequestURI();
if (request.getQueryString() != null)
    uri += "?" + request.getQueryString();

因此,当我浏览 http://google.com?q=abc 时,一切正常(正确).但是当我浏览 https://google.com 时出现问题.uri 的值为 http://google.com:443google.com:443,所以程序不仅在 HTTPS用过.

So, when I browse http://google.com?q=abc it is OK (correct). But there is problem when I browse https://google.com. The value of uri is http://google.com:443google.com:443, So the program doesn't only when HTTPS is used.

request.getRequestURL().toString() 的输出是一样的.

解决办法是什么?

推荐答案

按照设计,getRequestURL() 为您提供完整的 URL,仅缺少查询字符串.

By design, getRequestURL() gives you the full URL, missing only the query string.

HttpServletRequest,您可以使用以下方法获取 URI 的各个部分:

In HttpServletRequest, you can get individual parts of the URI using the methods below:

// Example: http://myhost:8080/people?lastname=Fox&age=30

String uri = request.getScheme() + "://" +   // "http" + "://
             request.getServerName() +       // "myhost"
             ":" +                           // ":"
             request.getServerPort() +       // "8080"
             request.getRequestURI() +       // "/people"
             "?" +                           // "?"
             request.getQueryString();       // "lastname=Fox&age=30"

  • .getScheme() 会给你 "https" 如果它是一个 https://domain 请求.
  • .getServerName()http(s)://domain 上给出 domain.
  • .getServerPort() 会给你端口.
    • .getScheme() will give you "https" if it was a https://domain request.
    • .getServerName() gives domain on http(s)://domain.
    • .getServerPort() will give you the port.
    • String uri = request.getScheme() + "://" +
                   request.getServerName() + 
                   ("http".equals(request.getScheme()) && request.getServerPort() == 80 || "https".equals(request.getScheme()) && request.getServerPort() == 443 ? "" : ":" + request.getServerPort() ) +
                   request.getRequestURI() +
                  (request.getQueryString() != null ? "?" + request.getQueryString() : "");
      

      上面的这段代码将获得完整的 URI,如果使用默认端口,则隐藏端口,如果未提供后者,则不添加 "?" 和查询字符串.

      This snippet above will get the full URI, hiding the port if the default one was used, and not adding the "?" and the query string if the latter was not provided.

      请注意,如果您的请求通过代理,您需要查看 X-Forwarded-Proto 标头,因为方案可能会被更改:

      Note, that if your request passes through a proxy, you need to look at the X-Forwarded-Proto header since the scheme might be altered:

      request.getHeader("X-Forwarded-Proto")
      

      另外,一个常见的头是X-Forwarded-For,它显示原始请求IP而不是代理IP.

      Also, a common header is X-Forwarded-For, which show the original request IP instead of the proxys IP.

      request.getHeader("X-Forwarded-For")
      

      如果您自己负责配置代理/负载均衡器,则需要确保在转发时设置这些标头.

      If you are responsible for the configuration of the proxy/load balancer yourself, you need to ensure that these headers are set upon forwarding.

      这篇关于在 Servlet 中获取 HTTP 和 HTTPS 请求的完整 URL 和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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