【问题标题】:Using Servlet API, how to determine if request was HTTP/1.0 or HTTP/1.1?使用 Servlet API,如何判断请求是 HTTP/1.0 还是 HTTP/1.1?
【发布时间】:2012-04-07 07:35:12
【问题描述】:

我正在修复一个错误,该错误仅在客户端使用 HTTP/1.0 时出现(并且秘密地​​,Internet Explorer 在防火墙后代理)。详情在这里:https://issues.apache.org/jira/browse/TAP5-1880

无论如何,正确的解决方案是在请求为 HTTP/1.0 时关闭一项功能(GZip 内容压缩)。但是,在搜索了 Servlet API 文档,甚至是 Jetty 源代码之后,我找不到任何暴露这些信息的地方。

那么,有没有办法确定这一点?我正在使用 Servlet API 2.5。

提前致谢!

【问题讨论】:

    标签: java http servlets


    【解决方案1】:
    request.getProtocol() will return "HTTP/1.0" or "HTTP/1.1"
    

    这里是例子,在你本地的tomcat中执行

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    
    public class ShowRequestHeaders extends HttpServlet {
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String title = "Servlet Example: Showing Request Headers";
        out.println(ServletUtilities.headWithTitle(title) +
                    "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                    "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                    "<B>Request Method: </B>" +
                    request.getMethod() + "<BR>\n" +
                    "<B>Request URI: </B>" +
                    request.getRequestURI() + "<BR>\n" +
                    "<B>Request Protocol: </B>" +
                    request.getProtocol() + "<BR><BR>\n" +
                    "<TABLE BORDER=1 ALIGN=CENTER>\n" +
                    "<TR BGCOLOR=\"#FFAD00\">\n" +
                    "<TH>Header Name<TH>Header Value");
        Enumeration headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()) {
          String headerName = (String)headerNames.nextElement();
          out.println("<TR><TD>" + headerName);
          out.println("    <TD>" + request.getHeader(headerName));
        }
        out.println("</TABLE>\n</BODY></HTML>");
      }
    
      public void doPost(HttpServletRequest request,
                         HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2011-02-15
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多