使用Servlet API,如何判断请求是HTTP/1.0还是HTTP/1.1?

问题描述 投票:0回答:1

我正在修复一个错误,该错误仅在客户端使用 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 httprequest
1个回答
11
投票
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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.