HTTP protocol _ (second lesson)

Source: Internet
Author: User
Tags apache tomcat

A Introduction to HTTP

1. Basic Introduction:

(1) After the client connects to the Web server, if it wants to obtain a Web resource in the Web server, it must follow a certain communication format, which is used to define the format of the client and Web server communication.

(2) A question-and-answer interaction between a Web browser and a Web server must follow a certain rule, which is the HTTP protocol.

(3)HTTP is a shorthand for the Hypertext Transfer Protocol(Hypertext Transfer Protocol), an application-layer protocol in the TCP/IP protocol that defines the WEB The process of exchanging data between a browser and a WEB server and the format of the data itself.

2. version of the HTTP protocol :http/1.0, http/1.1

3, the reason for learning the HTTP protocol: HTTP protocol is the cornerstone of learning javaweb development, do not understand the HTTP protocol, it can not be said to master the Web development, more unable to manage and maintain a number of complex web sites. An in-depth understanding of the HTTP protocol has a direct impact on managing and maintaining complex web sites and developing special-purpose Web server programs.

Two Session Mode for HTTP 1.0

1. Four steps of Session mode : establish connection, send request message, echo response information, close connection

comparative characteristics of 2.http1.0 and http1.1:

features of http1.0:

(1) The connection between the browser and the Web server is transient, and each connection only processes one request and response.

(2) for each page access, the browser and the Web server to establish a separate connection.

(3) All communications between the browser and the Web server are completely separate requests and responses.

(4) After the client has established a connection with the Web server, only one web resource can be obtained .

features of HTTP 1.1 :

(1) multiple HTTP requests and Responses can be delivered on a single TCP connection

(2) multiple request and response processes can overlap

(3) add more request headers and response headers

(4) When a client is allowed to connect to a Web server, it obtains multiple Web resources on one connection .

3. Use telnet to illustrate the steps:
⑴ enter telnet localhost 8080 and then enter

⑵ Press CTRL +]

⑶ send request, input ( format:(get/ path http/1.1)) get/news/1.html http/1.1

⑷ then enter the Host

Third, install the IE browser plugin httpwatch, view IE browser through the HTTP protocol to get a page.

use of HttpWatch tools

(1) function: listen and intercept the HTTP request data that the local machine sends to the server each time and the HTTP response data returned by the server to the local machine .

(2) Download: http://www.httpwatch.com/

(3) Directly double-click the download file installation.

(4) Open IE browser, select " tools "→" Explorer Bar "→"httpwatchprofessional".

(5) Once this is done, you can open the HttpWatch tool.

Iv. HTTP Request message:

1 . After the client connects to the server, it requests a Web resource, called the client, to send an HTTP request to the server.

2, the structure of the request message: a request line, a number of message headers, and entity content, some of the message headers and entity content are optional, The message header and entity content are separated by a blank line ( request line, message header, blank line, request data).

3.

3.1 request Line :

Used to describe how the client is requested, the name of the resource requested, and the version number of the HTTP protocol used

(1) Request method: POST,, OPTIONS, DELETE, TRACE, PUT

① commonly used are: GET, POST

If the user is not set, by default the browser sends a GET request to the server, For example, in the browser direct output address access, point hyperlink access and so on are get, the user wants to change the request mode to post, can be implemented by changing the form's submission method.

The ②get method can be used after the requested URL address . In the form of data to the server, with multiple data separated by & , for example:get/mail/1.html? name=abc&password=xyz http/1.1

Features: The parameters attached after the URL address are limited, and their data capacity usually cannot exceed 1K.

③post mode: You can send data to the server in the requested entity content.

Features: Unlimited amount of data transmitted.

④ Note: Regardless of whether post or get is used to request a Web resource to the server, the difference between the two approaches is primarily in data delivery

more than 3.2 message headers :

The message header is used to describe which host the client requests, and some of the client's environmental information, etc.

Message headers are used for common headers in HTTP requests

①accept: text/html,image/* describes the data formats that clients can receive

②accept-charset: Iso-8859-1 describes the character set that the client can use

③accept-encoding: gzip,compress Describes how the client can decode the data encoded

④accept-language: EN-US,ZH-CN used to specify which country language the client expects the server to return documents

⑤host: www.it315.org:80 used to specify the host number and port number where the resource resides

⑥if-modified-since: Tue, Jul 18:23:51 GMT for specified time

⑦referer: http://www.it315.org/index.jsp Specifies the URL of the source of the hyperlink that made the request

⑧user-agent: mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) is used to specify the type and name of the browser or other client program

⑨cookie clients can bring messages to the server side

⑩connection: Close/keep-alive tells the server if the request is complete and the server remains connected

11.Date: Tue, Jul 18:23:51 GMT

To illustrate:

get/books/java.html http/1.1 Request Line

Accept: */*

Accept-language:en-us

Connection:keep-alive

Host:localhost Multiple message headers

Referer:http://localhost/links.asp

user-agent:mozilla/4.0

Accept-encoding:gzip, deflate

a blank line

V.HTTP Response message:

1. An HTTP response represents the data that the server sends back to the client

2, Structure: a status lines, several message headers, and entity content. ( status line, message header, blank line, response data)

3.

Status line:

Used to describe the results of server-to-request processing

① format: HTTP version number status code reason description <CRLF>

Example: http/1.1 OK

The ② status code is used to represent the result of the server's processing of the request , which is a three-bit decimal number. The response status code is divided into 5 categories

Status code

Meaning

100~199

Indicates a successful receive request that requires the client to continue submitting the next request in order to complete the process

200~299

Indicates a successful receive request and has completed the entire process, typically 200

300~399

To complete the request, the customer needs to refine the request further. For example, the requested resource has moved a new address, commonly used 302, 307, and 304

400~499

Client request error, common 404

500~599

Server side error, common 500

Typical case of ③ response status codes

Ⅰ (normal) indicates that everything is normal and returns the normal request result.

Ⅱ 302/307(Temporary redirect) indicates that the requested document has been temporarily moved elsewhere, and the new URL for this document is given in the location response header.

Ⅲ 304(unmodified) indicates that the client cache version is up-to-date and the client should continue to use it.

The Ⅳ 403(Forbidden) Server understands the client request but refuses to process it. This is usually due to the permissions set on the file or directory on the server.

Ⅴ 404(not found) The resource requested by the client does not exist on the server.

Ⅵ (internal server error) The server side of the CGI, ASP, JSP, and other programs error occurred.

4.

Multiple message headers:

To describe the basic information of the server, as well as the description of the data, the server through the description of the data, you can tell the client how to handle the data that it sends back for a while

Common response Headers in the message header

(1) location:http://www.it315.org/index.jsp is used to inform the client that it should go to the new address to obtain the document

(2) Server:apache Tomcat product name for the specified server software

(3) Content-disposition:attachment; Filename=aaa.zip

(4) transfer-encoding:chunked

(5) Set-cookie:ss=q0=5lb_nq; Path=/search

(6) etag:w/"7777-1242234904000"

(7) Cache-control: No-cache

(8) Pragma: No-cache

(9) Connection: close/keep-alive

Date: Tue, Jul2000 18:23:51 GMT

In an HTTP response, the Web server describes the success of a client's request through a response header to the Web client, as well as information about the type of data it sends, which the client can then Know how to process the data

5.

Entity content:

data that is echoed back to the client on behalf of the server

Solid head:

(1) Content-encoding:gzip compression encoding for specifying entity content

(2) content-length:80 used to indicate the length of the entity content

(3) CONTENT-LANGUAGE:ZH-CN is used to specify the country language type of the page document that is returned.

(4) content-type:text/html; charset=gb2312 the MIME type specified in the Response header field (MIME type refers to the type of the entity content Compression encoded money)

(5) Last-modified:tue, Jul 18:23:51 GMT to specify the last change time for the document, set to GMT format

(6) Expires:-1 is used to specify when the current document should be considered expired

To illustrate:

http/1.1200 OK status line

server:microsoft-iis/5.0

Date:thu, Jul 05:46:53 GMT

content-length:2291 Multiple message headers

Content-type:text/html

Cache-control:private

A blank line

<HTML>

Entity content

<BODY>

......

Six, expansion head

In HTTP messages, you can also use header fields that are not defined in the HTTP 1.1 formal specification, which are called Custom http headers or extension headers, and they are often treated as a solid header.

Now popular browsers actually support cookies,set-cookie,Refresh and Content-disposition, and many more The common Extension header field.

The Refresh header field is used to tell the browser how many seconds after the page is automatically refreshed

Refresh:1

refresh:1;url=http://www.it315.org

The Content-disposition header field specifies how the receiving program handles the data content.

Content-type:application/octet-stream

Content-disposition:attachment;filename=aaa.zip

Content-disposition is an extension of the MIME protocol, and the MIME protocol indicates how the MIME user agent displays additional files. Content-disposition can actually control when the user requests the content to save as a file, provide a default file name, the file is displayed directly in the browser or the File Download dialog box when accessed.

Format Description:
Content-disposition = "Content-disposition" ":" Disposition-type * (";" Disposition-parm)

Field Description:

Content-disposition is a property name
How Disposition-type is downloaded, such as attachment for download by attachment
Disposition-parm is the name of the file when it is saved by default.

In web development, you may experience the following requirements:
l Want files of a certain class or a known MIME type (for example: *.gif;*.txt;*.htm) to be able to pop up the file Download dialog box when accessed.
L WANT the client to be displayed with the specified file name when downloading.
l want a file to appear directly on the browser instead of ejecting the File Download dialog box.
For the above requirements, use the Content-disposition property to resolve. The following is a code example:
Response.setheader ("Content-disposition", "attachment;filename=" + filename).
Content-disposition is the property name.
Attachment means to download as an attachment. If you want to open it in the page, change to inline.
If the filename is Chinese, garbled characters will appear. There are two ways to solve this problem:
1. Use FileName = new String (Filename.getbytes (), "iso8859-1") statement
2. Use the filename = httputility.urlencode (filename, System.Text.Encoding.UTF8) statement

HTTP protocol _ (second lesson)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.