有没有办法使用 Java 11 的 HttpClient 从 HTTP 1.1 响应的状态行中获取原因短语?



Java的java.net.HttpURLConnection有一个getResponseMessage((方法来检索任何Reason-Phrase文本(如RFC2616 6.1状态行所指定(。

在较新的Java 11 HttpClient中,java.net.http.HttpResponse只有一个statusCode((方法。

是否有方法使用此API获取状态行结尾返回的文本?如果没有,为什么不包括在内?(我知道HTTP2没有定义这个值,但大多数应用程序仍然使用HTTP1.1,它仍然有效。(

是否有方法使用此API获取状态行结尾返回的文本?

否。

如果没有,为什么不包括它?

我们无法告诉您实际原因,因为详细的设计原理尚未公布。

然而,我想原因/推理是这样的:

  1. 因为客户端经常忽略文本。(请耐心等待…(

  2. 因为文本中很少包含有用的内容。它通常只是与响应代码相对应的标准(推荐(文本消息。(这是客户端经常忽略文本的原因之一。(

  3. 因为如果一个web应用程序要提供一个有意义的解释,它很可能会在有效负载中这样做。(因为1。但也加强了它。(

  4. 因为有些网络堆栈不允许网络应用程序设置原因文本。

  5. 因为有些HTTP1.1 web服务器通常会完全忽略原因文本!(例如,对于Tomcat 8.0.x,没有消息,而对于Tomcat 8.5.x,有一个启用消息的选项。(


(我知道HTTP2没有定义这个值,但大多数应用程序仍然使用HTTP1.1,它仍然有效。(

现在1

这实际上是(新的(web应用程序不试图在原因文本中传递有用信息并且在API中不支持它的另一个原因。

最终,大多数应用程序将使用HTTP2或更高版本。或者至少,足够多的人会使用它,依赖对HTTP1.x特性的良好支持可能会给您的应用程序带来问题。

请记住,HTTP3现在正在酝酿中。没有人能预见到未来,准确预测在5年内,有多大比例的网络服务器将使用什么版本的HTTP。

1-服务器端采用率呈上升趋势,但因服务器平台而异。有关一些数据点,请参阅2020年网络年鉴


鉴于上述情况,如果您使客户端代码依赖于查看特定原因文本。。。或者看到任何。。。那么对于一些web服务器来说,它很容易崩溃。

因此,我想说的是,Java 11的HttpClient API设计器没有将原因文本暴露给客户端代码,这对我们大家都有很大帮助

你可以自由表达不同意见。。。并使用其他HTTP客户端库。


我的建议是顺其自然。如果你不尝试使用原因文本(客户端或服务器端(,那么你就不必处理使用它会带来的问题。问题只会变得更糟。

我在Java中寻找http状态码原因短语,在谷歌搜索中发现了这个讨论。我最终编写了自己的函数,我想分享它,以防其他人发现它有用。它可能不完整,但它符合我的目的。

public static String getReasonPhrase(int statusCode) {
switch(statusCode) {
case (200): return "OK";
case (201): return "Created";
case (202): return "Accepted";
case (203): return "Non Authoritative Information";
case (204): return "No Content";
case (205): return "Reset Content";
case (206): return "Partial Content";
case (207): return "Partial Update OK";
case (300): return "Mutliple Choices";
case (301): return "Moved Permanently";
case (302): return "Moved Temporarily";
case (303): return "See Other";
case (304): return "Not Modified";
case (305): return "Use Proxy";
case (307): return "Temporary Redirect";
case (400): return "Bad Request";
case (401): return "Unauthorized";
case (402): return "Payment Required";
case (403): return "Forbidden";
case (404): return "Not Found";
case (405): return "Method Not Allowed";
case (406): return "Not Acceptable";
case (407): return "Proxy Authentication Required";
case (408): return "Request Timeout";
case (409): return "Conflict";
case (410): return "Gone";
case (411): return "Length Required";
case (412): return "Precondition Failed";
case (413): return "Request Entity Too Large";
case (414): return "Request-URI Too Long";
case (415): return "Unsupported Media Type";
case (416): return "Requested Range Not Satisfiable";
case (417): return "Expectation Failed";
case (418): return "Reauthentication Required";
case (419): return "Proxy Reauthentication Required";
case (422): return "Unprocessable Entity";
case (423): return "Locked";
case (424): return "Failed Dependency";
case (500): return "Server Error";
case (501): return "Not Implemented";
case (502): return "Bad Gateway";
case (503): return "Service Unavailable";
case (504): return "Gateway Timeout";
case (505): return "HTTP Version Not Supported";
case (507): return "Insufficient Storage";
default: return "";
}
}

正如您所注意到的,HttpResponse没有提供任何API来获取原因短语。没有隐藏的方法可以获得它。你有没有一个用例,在HttpResponse中为原因短语设置访问器会很有用?

最新更新