如何定义一个HTTP响应呢?在RFC2616的第六章定义了HTTP Response的格式。请参考官网。
HTTP响应的定义格式:
Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ]
状态行的定义:
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
状态代码和状态原因:
Status-Code = "100" Continue | "101" Switching Protocols | "200" OK | "201" Created | "202" Accepted | "203" Non-Authoritative Information | "204" No Content | "205" Reset Content | "206" Partial Content | "300" Multiple Choices | "301" Moved Permanently | "302" Found | "303" See Other | "304" Not Modified | "305" Use Proxy | "307" Temporary Redirect | "400" Bad Request | "401" Unauthorized | "402" Payment Required | "403" Forbidden | "404" Not Found | "405" Method Not Allowed | "406" Not Acceptable | "407" Proxy Authentication Required | "408" Request Time-out | "409" Conflict | "410" Gone | "411" Length Required | "412" Precondition Failed | "413" Request Entity Too Large | "414" Request-URI Too Large | "415" Unsupported Media Type | "416" Requested range not satisfiable | "417" Expectation Failed | "500" Internal Server Error | "501" Not Implemented | "502" Bad Gateway | "503" Service Unavailable | "504" Gateway Time-out | "505" HTTP Version not supported | extension-code extension-code = 3DIGIT Reason-Phrase = *<TEXT, excluding CR, LF>
上面的状态码可以按第一个数字分为5大类别的状态:信息,成功,重定向,客户端错误,服务器端错误。如下:
- 1xx: Informational - Request received, continuing process - 2xx: Success - The action was successfully received, understood, and accepted - 3xx: Redirection - Further action must be taken in order to complete the request - 4xx: Client Error - The request contains bad syntax or cannot be fulfilled - 5xx: Server Error - The server failed to fulfill an apparently valid request
响应头字段的定义:
response-header = Accept-Ranges | Age | ETag | Location | Proxy-Authenticate | Retry-After | Server | Vary | WWW-Authenticate