HTTP request Model

Source: Internet
Author: User
Tags rfc

1. Connect to the Web server
A client application (such as a web browser) opens a socket (80 by default) to the http port of the Web server ).

Example: http://www.myweb.com: 8080/index.html
In Java, this is equivalent to the Code:
Soceet socket = new socket ("www.myweb.com", 8080 );
Inputstream in = socket. getinputstream ();
Outputstream out = socket. getoutputstream ();

Ii. Send an HTTP request
Through connection, the client writes an ASCII text request line followed by zero or more HTTP header labels, one blank line and any data requested.
A request consists of four parts: request line, request header mark, blank line, and request data
1. Request Line: The request line consists of three tags: Request Method, request URI, and HTTP Version. They are separated by spaces.
Example: Get/index.html HTTP/1.1
The HTTP specification defines eight possible request methods:
Get retrieves a simple request that identifies a resource in a URI.
The head method is the same as the get method. The server only returns the status line and header label, and does not return the request document.
The Post Server accepts requests for data written into the output stream of the client.
The put server saves the request data as a request to specify the new content of the URI.
Delete server request to delete the resource named in URI
Options requests for server-supported request methods
The trace web server provides feedback on HTTP requests and headers.
Connect is a documented but not implemented method. It is reserved for tunnel processing.
2. Request Header: it consists of key/value pairs. Each line has a pair. The keywords and values are separated by colons.
The request header is used to notify the server of the functions and identifiers of the client. A typical request header is marked:
User-Agent client manufacturer and version
List of content types that can be recognized by the accept client
Number of bytes of Content-Length appended to the request
3. blank line: the last request header is followed by an empty line, which sends a carriage return and a fallback message, notifying the server that there is no header mark below.
4. Request data: the Content-Type and Content-Length headers are usually used for data transmission using post.

3. The server accepts the request and returns an HTTP Response
The Web server parses the request and locates the specified resource. The server writes a copy of the resource to the socket, which is read by the client.
A response consists of four parts: Status line, Response Header mark, blank line, and response data.
1. Status line: the Status line consists of three tags: HTTP Version, response code, and response description.
HTTP Version: specify the maximum version that the client can understand.
Response Code: A three-digit numeric code that indicates whether the request succeeds or fails. If the request fails, it indicates the reason.
Response Description: the readability of the response code.
Example: HTTP/1.1 200 OK
HTTP response code:
1xx: information, request received, continue processing
2XX: Successful, accepted, understood, and accepted
3xx: redirection. actions that must be performed further to complete the request
4xx: client error:
2. Response Header: like the request header, they indicate the functions of the server and identify the details of the response data.
3. blank line: the last response header is followed by an empty line. The carriage return and fallback are sent, indicating that no header mark is available below the server.
4. Response Data: HTML documents and images, that is, HTML itself.

4. The server closes the connection and the browser parses the response.
1. the browser first parses the status line to check the status code indicating whether the request is successful.
2. parse each response header, and the header Mark tells the following several bytes of HTML.
3. Read the response data HTML, format it according to the syntax and semantics of the HTML, and display it in the browser window.
4. An HTML document may contain references to other resources to be loaded. the browser recognizes these references and makes additional requests to other resources. This process is repeated multiple times.

5. Stateless connection
The HTTP model is stateless, indicating that the Web server does not remember requests from the same client when processing a request.

Vi. Instances
1. the browser sends a request
GET/index.html HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Content-Length: 119
Content-Type: text/html

<HTML>
<Head>
<LINK rel = "stylesheet" href = "index.css">
</Head>
<Body>

</Body>
</Html>

2. the browser sends a request
Get.index.css HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 70
Content-Type: text/Plane

H3 {
Font-size: 20px;
Font-weight: bold;
Color: # 005a9c;
}

3. the browser sends a request
Get image/logo.png HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 1280
Content-Type: text/Plane

{Binary image data follows}

(Appendix)
1. HTTP specification: RFC published by the Internet Engineering Development Organization (IETF) specifies Internet standards, which are widely accepted by Internet research and development institutions. Because they are standard documents, they are generally written in formal languages, like the legislative document.
2. RFC: once an RFC is proposed, it is numbered and will not be changed. When a standard is modified, a new RFC is given. As a standard, RFC is widely used on the Internet.
3. Several important RFC for http:
Rfc1945 HTTP 1.0 description
Rfc2068 initial description of HTTP 1.1
Rfc2616 standard for HTTP 1.1
4. Resource Identifier uri (Uniform Resource identifter, Uri)

 

HTTP protocol

 

The basic protocol of the Internet is the TCP/IP protocol. For example, FTP and HTTP are the application layer protocols built on the TCP/IP protocol, the main protocol used by the WWW server is HTTP (Hypertext Transfer Protocol), and the services supported by HTTP are not limited to www.
HTTP protocol features: Support for customer/Server mode, no connection, no status.
The operation mode is based on the request/response paradigm. The internal operation process is as follows:
1. Establishing a connection is a relative concept between the customer and the server. When the WWW server is running, it keeps listening on the tcp80 port (the default WWW port), waiting for the connection to appear. The connection is established by applying for a socket. The customer opens a socket and limits it to a port. If it succeeds, it is equivalent to creating a virtual file. Operations on virtual files.
2. Send a request. After a connection is opened, the client sends the request message to the server's Stop port to complete the request action.
The request format is:
Request Message = request line (Common Information | Request Header | entity header) CRLF [entity content]
Request Line = method request url http Version Number CRLF
Method = GET | HEAD | post | Extension Method
U r l = protocol name + host name + directory and file name
Head -- requires the server to find the metadata of an object, not the object itself.
Post: transmits data from a client to the server. The post method is used when the server and CGI are required for further processing. Post is mainly used to send Form Content in HTML text for CGI programs to process.
An example of a request is:
Get http://networking.zju.edu.cn/zju/index.htm HTTP/1.0
Header information is also known as metadata, that is, information. meta information can be used to implement conditional requests or responses.
Request Header-tells the server how to interpret the request, including the data type, compression method, and language that the user can accept.
Object Header: Object Information type, length, compression method, last modification time, data validity period, etc.
Entity-request or response object itself.
3. Send a response. The server sends a Response Message to the client after processing the customer's request.
Response Message format of HTTP/1.0:
Response Message = Status line (Common Information header | response header | entity header) CRLF [entity content 〕
Status line = HTTP Version Number status code reason description
The response header information includes the service program name, the URL that notifies the customer of the request to be authenticated, and the requested resource when it can be used.
4. Close the connection: both the client and the server can end the TCP/IP conversation by closing the socket.



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.