简体   繁体   中英

Override the HTTP response status text

How can I override in Tomcat 7 the text of the HttpStatu s.

I'm using HttpServletResponse.sendError(401, "Invalid username or Password") , but when I'm looking at the response status in the client it goves 401 Unauthorized .

Is there any way to override it?

Tomcat no longer supports USE_CUSTOM_STATUS_MSG_IN_HEADER property.

Changelog from 8.5.0 :

RFC 7230 states that clients should ignore reason phrases in HTTP/1.1 response messages. Since the reason phrase is optional, Tomcat no longer sends it. As a result the system property org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER is no longer used and has been removed. (markt)

RFC 7230, Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing , June 2014. Section 3.1.2 :

The reason-phrase element exists for the sole purpose of providing a textual description associated with the numeric status code, mostly out of deference to earlier Internet application protocols that were more frequently used with interactive text clients. A client SHOULD ignore the reason-phrase content .

否 - 响应代码根据RFC 2616设置。如果要将消息传递给用户(到API客户端),请将其写入正文或响应头中

Edit catalina.properties and add the property:

org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER=true

With that set in my dev environment, then when I do:

response.sendError(HttpServletResponse.SC_BAD_REQUEST, 
                   "A very very very bad request");

I see:

HTTP/1.1 400 A very very very bad request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1024
Date: Fri, 20 Dec 2013 11:09:54 GMT
Connection: close

Also discussed here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM