HTTP server development (Java) -- HTTP request, java -- http

Source: Internet
Author: User
Tags apache tomcat

HTTP server development (Java) -- HTTP request, java -- http

Recently, I want to create an HTTP server for my course assignments. Now I want to record the entire process of my course assignments.

(1) theoretical knowledge

HTTP (HyperText Transfer Protocol) is a set of rules for computers to communicate over the network. Computer experts have designed HTTP to enable HTTP clients (such as Web browsers) to request information and services from HTTP servers (Web servers, the current version of HTTP protocol is 1.1.HTTP, which is a stateless protocol. Stateless means that a persistent connection does not need to be established between a Web browser and a Web server, this means that when a client sends a request to the server and then the Web server returns a response (response), the connection is closed and the server does not retain the connection information. HTTP follows the Request/Response model. The Web browser sends a request to the Web server. The Web server processes the request and returns an appropriate response. All HTTP connections are constructed into a set of requests and responses.

The content type used by HTTP means that all files returned by the Web server to the Web browser are of the relevant type. All these types are modeled on the MIME Internet mail protocol, that is, the Web server tells the Web browser of the file type, whether it is an HTML document, GIF format image, sound file or an independent application. Most Web browsers have a series of configurable third-party applications that tell the browser how to handle various types of content sent from the Web server.
In a complete HTTP communication process, the following seven steps are completed between the Web browser and the Web server:
(1) Establish a TCP connection
Before HTTP starts, the Web browser first needs to establish a connection with the Web server through the network. The connection is completed through TCP. The protocol and the IP protocol jointly build the Internet, the famous TCP/IP protocol family, so the Internet is also called a TCP/IP network. HTTP is a higher-level application layer protocol than TCP. according to rules, only a lower-layer protocol can be established before connection to a higher-layer protocol can be established. Therefore, a TCP connection must be established first, generally, the TCP connection port is 80.
(2) The Web browser sends request commands to the Web server
Once a TCP connection is established, the Web browser sends the Request command to the Web server.
Example: GET/sample/hello. jsp HTTP/1.1
(3) The Web browser sends the request header information
After the browser sends the Request command, it also sends some other information to the Web server in the form of header information. Then, the browser sends a blank line to notify the server, it has ended sending this header.
(4) Web Server Response
After the client sends a request to the server, the server returns the response from the client,
HTTP/1.1 200 OK
The first part of the response is the Protocol version number and response status code.
(5) The Web server sends response header information
Just as the client sends information about itself along with the request, the server also sends a document about its own data and the request to the user along with the response.
(6) The Web server sends data to the browser
After the Web server sends the header information to the browser, it will send a blank line to indicate that the header information is sent to this end, and then, it sends the actual data requested by the user in the format described in the Content-Type response header information.
(7) disable TCP connection on the Web Server
In general, once the Web server sends the request data to the browser, it must close the TCP connection. Then, if the browser or Server adds this line of code to its header information
Connection: keep-alive
The TCP connection remains open after being sent. Therefore, the browser can continue to send requests through the same connection. Keeping connections saves the time required to create new connections for each request and reduces network bandwidth.
    
HTTP request format
When a browser sends a request to the Web server, it sends a data block to the server, that is, the request information. The HTTP request information consists of three parts:
1. Request Method URI protocol/version
2. Request Header)

3. Request body

HTTP Request Message format:

GET/Webpage/test HTTP/1.1

Host: 127.0.0.1: 8888

Connection: keep-alive

Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp,/*/; q = 0.8

User-Agent: Mozilla/5.0 (Macintosh; IntelMac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36

Accept-Encoding: gzip, deflate, sdch

Accept-Language: zh-CN, zh; q = 0.8

Q is the weight coefficient. The value range is 0 = <q <= 1. The larger the q value, the more requests tend to get the content indicated by the type prior,

If the q value is not specified, the default value is 1. If the value is 0, it is used to remind the server which content types are not accepted by the browser.

The request header is an empty line between the request header and the request body. This line is very important. It indicates that the request header has ended, followed by the request body. The request body can contain the query string information submitted by the customer:
Username = jinqiao & password = 1234
In the preceding HTTP request, the request body contains only one line of content. Of course, in actual applications, the HTTP Request body can contain more content.
I will only discuss the GET and POST methods here.
(1) 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 is only encoded, at the same time, it will be sent to the Web server as part of the URL. Therefore, if you use the GET method to submit form data, there is a security risk. For example
Http: // 127.0.0.1: 8888/index.html? Name = lip
From the preceding URL request, you can easily recognize the content submitted by the form. (? In addition, because the data submitted by the GET method is a part of the URL request, the amount of data submitted cannot be too large.
(2) POST Method
The POST method is an alternative to the GET method. It mainly submits form data to the Web server, especially a large volume of data. The POST method overcomes some shortcomings of the GET method. When the form data is submitted through the POST method, the data is not part of the URL request but is transmitted to the Web server as standard data, which overcomes the disadvantages of the information in the GET method being unable to be kept confidential and the data volume is too small. Therefore, for security considerations and respect for user privacy, the POST method is usually used for form submission.

The HTTP response is similar to the HTTP request. The HTTP response consists of three parts:
(1) Protocol Status version Code Description
(2) Response Header)
(3) response body
The following is an example of an HTTP response:

HTTP/1.1 200 OK

Connection: keep-alive

Server: Apache Tomcat/5.0.12

Date: Mon, 6Oct2003 13:23:42 GMT

Last-Modified: Mon, 6Oct2003 13:23:42 GMT

Content-Length: 112

Content-Type: text/html

<Html>

<Head>

<Title> HTTP response example <title>

</Head>

<Body>

Hello HTTP!

</Body>

</Html>

The Protocol Status Code describes the first line of the HTTP response, which is similar to the first line of the HTTP request, it indicates that the protocol used for communication is that the HTTP1.1 server has successfully processed the request sent by the client (200 indicates that the request was successful ):
HTTP/1.1 200 OK
The Response Header also contains a lot of useful information like the request Header, such as the server type, date and time, content type, and length.


HTTP response code
The HTTP response code is also called a status code, which reflects the status of the Web server processing HTTP requests. An HTTP response code consists of three digits. the first digit defines the type of the response code:

1XX-Information indicates that a Web browser request is received and is being processed.

2XX-Successful indicates that user requests are correctly received, understood, and processed, for example, 200 OK.

3XX-Redirection class (Redirection), indicating that the request failed and the customer must take further actions.

4XX-Client Error indicates that a request submitted by the Client has an Error such as 404 NOTFound, which means that the document referenced in the request does not exist.

5XX-Server Error indicates that the Server cannot process the request: for example, 500


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.