http message header

http message header

See the http://www.w3.org/Protocols/ specification for a detailed and complete description of HTTP headers.    

1. HTTP headers

(1) General information header

It can be used in the request message or in the response message, but it has nothing to do with the content of the transmitted entity, such as Data, Pragma

主要: Cache-Control , Connection , Data , Pragma , Trailer , Transfer-Encoding , Upgrade

(2) Request header

It is used to transmit additional information to the server in the request message, mainly including the data type acceptable to the client, the compression method, the language, and the information retained on the client computer and the source address of the hyperlink that issued the request.

主要: Accept , Accept-Encoding , Accept-Language , Host ,

(3) Response header

Used to pass additional information to the client in the response message, including the name of the service program, how the client was required to authenticate, the requested resource has been moved to a new address, etc.

Main: Location , Server , WWW-Authenticate (authentication header)

(4) Entity head

It is used as the meta information of the entity content and describes the attributes of the entity content, including the type, length, compression method, last modification time and data validity period of the entity information.

主要: Content-Encoding , Content-Language , Content-Length , Content-Location , Content-Type

(4) Extension head

Main: Refresh, Content-Disposition

2. The role of several main heads

(1) The role of Content-Type

The purpose of this entity header is to let the server tell the browser what file type the data it is sending belongs to.

For example: when the value of Content-Type is set to text/html and text/plain, the former will let the browser parse the received entity content in HTML format, and the latter will let the browser parse it in normal text.

(2) The role of Content-Disposition

When the type of Content-Type is the type to be downloaded, this header will tell the browser the name and type of the file.

When explaining this content, Mr. Zhang also talked about the solution to solve the garbled Chinese file name. What he usually thinks is to use getBytes(), in fact, the file name should be encoded using the email attachment name encoding method, but IE does not support it. In this way (supported by other browsers), use the MimeUtility.encodeWord("Chinese.txt") method of the javax.mail.internet.* package for encoding.

Example of Content-Disposition extension header:

<%@ page pageEncoding="GBK" contentType="text/html;charset=utf-8" import="java.util.*,java.text.*" %>

<%=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.CHINA).format(new Date())

%>

<%

               response.setHeader("Content-Type","video/x-msvideo");

               response.setHeader("Content-Disposition", "attachment;filename=aaa.doc");

%>

The type specified in Content-Disposition is the extension of the file, and the file type image in the pop-up download dialog is displayed according to the extension of the file. After clicking Save, the file is named with the value of filename, and the save type is set in Content. shall prevail.

Note: Be sure to set the Content-Type header field before setting the Content-Disposition header field.

(3)Authorization头的作用

Authorization的作用是当客户端访问受口令保护时,服务器端会发送401状态码和WWW-Authenticate响应头,要求客户机使用Authorization来应答。

例如:

<%@ page pageEncoding="GBK" contentType="text/html;charset=utf-8" import="java.util.*,java.text.*" %>

<%=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.CHINA).format(new Date())

%>

<%

response.setStatus(401);

response.setHeader("WWW-Authenticate", "Basic realm=/"Tomcat Manager Application/"");

%>

3.如何实现文件下载

要实现文件下载,我们只需要设置两个特殊的相应头,它们是什么头?如果文件名带中文,该如何解决?

两个特殊的相应头:

----Content-Type:       application/octet-stream

----Content-Disposition: attachment;filename=aaa.zip


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584194&siteId=291194637