Minimalism technology Jane recorded -HTTP

HTTP Profile

HTTP protocol (HyperText Transfer Protocol, Hypertext Transfer Protocol) is the most widely used on the Internet A network transport protocol, all WWW documents must comply with this standard.

HTTP is used to transfer a data (HTML files, image files, query results, etc.) based on TCP / IP communication protocol.

HTTP works

HTTP protocol works on the client - server architecture on. Browser as an HTTP client URL that is WEB server sends all requests to the server via HTTP.

There are Web servers: Apache server, IIS server (Internet Information Services) and so on.

Web server according to the received request, transmits the response information to the client.

The default HTTP port number is 80, but you can also be changed to 8080 or other ports.

HTTP Three things to note:

HTTP is a connectionless: Meaning No limitation is attached only one request per connection. After the server processes client requests and receives the customer's response, i.e., disconnected. In this way it can save transmission time.

HTTP is an independent media: This means that, as long as the client and the server knows how to handle the data content of any type of data can be sent via HTTP. The client and server specify the appropriate MIME-type content type.

HTTP is a stateless: HTTP protocol is stateless protocol. No state is no protocol for transaction processing and memory. If the lack of state means that the subsequent processing required in front of the information, it must be retransmitted, which may result in the amount of data transmitted for each connection is increased. On the other hand, when it does not require previous information in response to a faster server.

HTTP protocol communication flow:

HTTP message structure

HTTP is the client / server (C / S) model framework to exchange information via a secure link is a stateless request / response protocol.

A HTTP "client" is an application (Web browser or any other client), the purpose of the transmission of one or more HTTP request to the server by connecting to the server.

A HTTP "server" is also an application (typically a Web service, such as the Apache Web server or IIS server, etc.), by receiving the client's request is sent to the client HTTP response data.

HTTP uses uniform resource identifier (Uniform Resource Identifiers, URI) to transmit data and establish a connection.

Once the connection is established, data on a similar message format used by the Internet Mail [RFC5322] and Multipurpose Internet Mail Extensions (MIME) [RFC2045] is transmitted.

Client request message

The client sends a HTTP request to the server request message comprises the following format: request line (request line), the request header (header), and a blank line composed of four data request .

Server response message

HTTP response also consists of four parts, namely: a status line, message header, and response body blank line.

The method of HTTP request

According to the HTTP standard, HTTP request can use several request methods.

HTTP1.0 request defines three methods: GET, POST, and HEAD method.

HTTP1.1 six new request methods: OPTIONS, PUT, PATCH, DELETE, TRACE, and CONNECT method.

No.

method

description

1

GET

Request page information specified, and returns the entity body.

2

HEAD

Similar to the GET request, returns a response but not the specific content, for obtaining the header

3

POST

Submitting data to the processing request specified resource (e.g., file submission form or upload). Data contained in the request body. POST request may result in a revision to establish and / or existing resources to new resources.

4

PUT

Data transmitted from the client to the server designated to replace the contents of the document.

5

DELETE

Requests the server to delete the specified page.

6

CONNECT

HTTP / 1.1 protocol can be reserved for connection to the proxy server pipeline mode.

7

OPTIONS

It allows the client to view server performance.

8

TRACE

Echo request received by the server, mainly for testing or diagnosis.

9

PATCH

It complements the PUT method, used locally known resource update.

 

HTTP response headers

Response header

Explanation

Allow

The method that the server supports the request (such as GET, POST, etc.).

Content-Encoding

Document encoding (Encode) method. Only after decoding can get content types Content-Type header specified. Use gzip compressed files can significantly reduce the download time of HTML documents. Java's GZIPOutputStream gzip compression can be easily, but only Netscape and IE on Windows on Unix 4, IE 5 is supported it. Therefore, Servlet should be by looking at the Accept-Encoding header (ie request.getHeader ( "Accept-Encoding")) to check whether the browser supports gzip, gzip compressed by the return HTML pages to browsers support gzip, return to normal as other browsers page.

Content-Length

It represents the content length. Only when this data is required if the browser is using a persistent HTTP connections. If you want to take advantage of persistent connections, you can write the output document ByteArrayOutputStream, after the completion of view of its size, and then put that value into the Content-Length header, and finally sent content via byteArrayStream.writeTo (response.getOutputStream ().

Content-Type

It indicates that the following documents belong to what MIME type. Servlet default is text / plain, but usually need to explicitly specify text / html. Since often provided Content-Type, thus providing a dedicated HttpServletResponse method setContentType.

Date

The current GMT time. You can set this setDateHeader head to avoid trouble conversion time format.

Expires

When you should believe the document has expired, eliminating cache it?

Last-Modified

The last time changes to the document. Customers can provide through the If-Modified-Since request header a date, the request will be considered as a condition GET, only changes to documents later than the specified time will be returned, otherwise state a 304 (Not Modified). Last-Modified setDateHeader method can also be set.

Location

Customers should indicate where to extract the files. Location usually not directly, but through sendRedirect HttpServletResponse method, the method while setting the status code to 302.

Refresh

It represents the browser should refresh the document after how much time, in seconds. In addition to refreshing addition to the current document, you can also setHeader ( "Refresh", "5 ; URL = http: // host / path") let the browser reads the specified page.
Note that this feature is usually by setting the HTML page HEAD area <META HTTP-EQUIV = "Refresh " CONTENT = "5; URL = http: // host / path"> achieved because, for the automatic refresh or redirect those who can not use CGI or Servlet writers of HTML is very important. However, Servlet, the head directly Refresh easier.
Note Refresh is the meaning of "after N seconds or refresh the page to access the specified page" instead of "every N seconds or refresh the page to access the specified page." Therefore, the continuous refresh required each time to send a Refresh header, and send 204 status code, you can prevent the browser continues to refresh, use the Refresh either the head or the <META HTTP-EQUIV = "Refresh " ...>.
Note Refresh header is not part of the official HTTP 1.1 specification, but rather an extension, but Netscape and IE support it.

Server

服务器名字。Servlet一般不设置这个值,而是由Web服务器自己设置。

Set-Cookie

设置和页面关联的Cookie。Servlet不应使用response.setHeader("Set-Cookie", ...),而是应使用HttpServletResponse提供的专用方法addCookie。参见下文有关Cookie设置的讨论。

WWW-Authenticate

客户应该在Authorization头中提供什么类型的授权信息?在包含401(Unauthorized)状态行的应答中这个头是必需的。例如,response.setHeader("WWW-Authenticate", "BASIC realm=\"executives\"")。
注意Servlet一般不进行这方面的处理,而是让Web服务器的专门机制来控制受密码保护页面的访问(例如.htaccess)。

 

HTTP状态码

当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求。

HTTP状态码的英文为HTTP Status Code。

下面是常见的HTTP状态码:

200 - 请求成功

301 - 资源(网页等)被永久转移到其它URL

404 - 请求的资源(网页等)不存在

500 - 内部服务器错误

HTTP状态码分类

HTTP状态码由三个十进制数字组成,第一个十进制数字定义了状态码的类型,后两个数字没有分类的作用。HTTP状态码共分为5种类型:

分类

分类描述

1**

信息,服务器收到请求,需要请求者继续执行操作

2**

成功,操作被成功接收并处理

3**

重定向,需要进一步的操作以完成请求

4**

客户端错误,请求包含语法错误或无法完成请求

5**

服务器错误,服务器在处理请求的过程中发生了错误

 

HTTP content-type

Content-Type(内容类型),一般是指网页中存在的 Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件,这就是经常看到一些 PHP 网页点击的结果却是下载一个文件或一张图片的原因。

Content-Type 标头告诉客户端实际返回的内容的内容类型。

语法格式:

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something

 

 常见的媒体格式类型如下:

    text/html : HTML格式

    text/plain :纯文本格式

    text/xml : XML格式

    image/gif :gif图片格式

    image/jpeg :jpg图片格式

    image/png:png图片格式

application开头的媒体格式类型:

    application/xhtml+xml :XHTML格式

    application/xml: XML数据格式

    application/atom+xml :Atom XML聚合格式

    application/json: JSON数据格式

    application/pdf:pdf格式

    application/msword : Word文档格式

    application/octet-stream : 二进制流数据(如常见的文件下载)

    application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

另外一种常见的媒体格式是上传文件之时使用的:

    multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式

Guess you like

Origin www.cnblogs.com/loytime/p/11883138.html