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

查看:1227
本文介绍了在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 ,所以该程序没有' t仅在使用 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://域 request。

  • .getServerName()给出 http(s)://域

  • .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天全站免登陆