HTTP layman Author: My Heart Will Go Source: blogjava.net Hits: 747 Update Time: 2005-11-16 【Font: Small Big】
If you have any questions, please ask questions on the development forum.
script>
HTTP (Hypertext Transfer Protocol is a set of rules that communicate over the network. Computer experts have designed HTTP to make HTTP customers (such as web browsers) to request information and services from HTTP Server (Web Server). The version of HTTP current protocol is 1.1.http is a stateless protocol, and there is no state means web There is no need to establish a lasting connection between the browser and the web server, which means that a client will issue a request to the server side, then the web server returns a response (response), the connection is turned off, and the server is not retained related Information. HTTP follows the request / response model. The web browser sends a request to the web server, the web server handles the request and returns the appropriate response. All HTTP connections are constructed into a set of requests and answers.
HTTP uses the content type to refer to the type of file returned to the web browser to the web browser. All of these types are modeled on the MIME Internet Mail Protocol, that is, the web server tells the web browser that the type of the file is the HTML document, GIF format image, sound file or independent application. Most web browsers have a range of configurable auxiliary applications that tell the browser how to handle the various content types sent by the web server.
HTTP communication mechanism is in a complete
During the HTTP communication, the following seven steps will be completed between the web browser and the web server:
(1)
set up
TCP connection
in
Before the HTTP work, the web browser first has to establish a connection with the web server through the network. This connection is done by TCP. The protocol works with the IP protocol to build an Internet, which is the famous TCP / IP protocol, so the Internet is also called It is a TCP / IP network. HTTP is a higher-level application layer protocol than TCP. According to the rules, only the low-level protocol is only available, and the connection of the over-layer protocol can be connected, so the first TCP connection, the general TCP connection port number is 80
(2)
Web browser sends a request command to the web server
Once established
TCP connection, web browser sends a request command to the web server
E.g:
Get / sample / hello.jsp http / 1.1
(3)
Web browser sends a request header information
After sending its request command, the browser sends its request command.
The web server sends some other information, and then the browser sends a blank line to notify the server, which has ended the sending of the information.
(4)
Web server response
After the client has issued a request to the server, the server will send a response to the client.
HTTP / 1.1 200 ok
The first part of the response is the version number and response status code of the agreement.
(5)
Web server send answering head information
Just as the client will request to send the information about its own information, the server will also answer the user's own data and the requested document.
(6)
Web server sends data to your browser
After the web server transmits the header information to the browser, it sends a blank line to indicate the transmission of the header information to the end, then it sends the actual data requested by the user in the format described by the Content-Type response header information ( 7)
Web server closes TCP connection
Under normal circumstances, once
The web server sends the request data to the browser, it will turn off the TCP connection, then if the browser or server adds this line of code at its header information
Connection: Keep-alive
The TCP connection will remain open after sending, so that the browser can continue to send a request by the same connection. Keep the connection saves the time required to establish a new connection for each request, and saves network bandwidth.
HTTP request format
When the browser
When the web server issues a request, it passes a data block to the server, which is requested information.
The HTTP request information consists of 3 parts:
l
Request method
URI protocol / version
l
Request head
(Request Header)
l
Request text
Here is one
Example of HTTP request:
Get / sample.jsphttp / 1.1
Accept: image / gif.image / jpeg, * / *
Accept-language: zh-cn
Connection: Keep-alive
Host: Localhost
User-agent: mozila / 4.0 (compatible; msie5.01; window nt5.0)
Accept-encoding: Gzip, deflate
UserName = jinqiao & password = 1234
(1)
Request method
URI protocol / version
The first line of request is "method
URL / version ": get / sample.jsp http / 1.1
"In the above code"
Get "Represents Request Method," / Sample.jsp "Represents URI," HTTP / 1.1 represents the version of the protocol and protocol.
according to
HTTP standard, HTTP requests can use multiple request methods. For example: HTTP1.1 supports seven request methods: GET, POST, HEAD, OPTIONS, PUT, DELETE, and TARCE. In Internet applications, the most common method is GET and POST.
The URL completely specifies the network resources to access, and usually simply give the relative directory of the root directory of the server, therefore always begins with "/", and finally, the protocol version declares that the communication process uses HTTP version.
(
2) Request Header
The request header contains many related client environments and useful information for requesting text. For example, the request head can declare the language used by the browser, request the length of the text.
Accept: image / gif.image / jpeg. * / *
Accept-language: zh-cn
Connection: Keep-alive
Host: Localhost
User-agent: mozila / 4.0 (compatible: msie5.01: windows nt5.0)
Accept-encoding: gzip, deflate.
(
3) Request the text
The request header and the request body are a blank line, this line is very important, it indicates that the request head has ended, and the next is the request text. Request the text that can contain the query string information submitted by the customer:
UserName = jinqiao & password = 1234
In the above example
In the HTTP request, the text requested only one line of content. Of course, in practical applications, the HTTP request text can contain more content.
HTTP request method I only discuss here
GET method and post method L
Get method
The GET method is the default HTTP request method. We use the get method to submit form data, however, the form data submitted by the GET method only has passed, and it will send a part of the URL to the web server, so if used There is a safety hazard on the GET method to submit form data. E.g
Http://127.0.0.1/login.jsp?name=zhangshi&age=30&subsmit=