HTTP Server Development (Java)--http requests

Source: Internet
Author: User

Recently due to the course work, to do an HTTP server, now record the course I do the whole process of the homework.

(a) theoretical knowledge

HTTP (hypertext Transfer Protocol) is a set of rules for computers to communicate over a network. Computer experts design HTTP to enable HTTP clients (such as Web browsers) to request information and services from an HTTP server (Web server). HTTP Current protocol version is 1.1.HTTP is a stateless protocol, stateless refers to the Web browser and the Web server does not need to establish a persistent connection, which means when a client makes a request to the server, and then the Web server returns a response (response), The connection is closed and information about the connection is not maintained on the server side. HTTP follows the request/answer (Response) model. The Web browser sends a request to the Web server, and the Web server processes the request and returns the appropriate answer. All HTTP connections are constructed as a set of requests and responses.

HTTP uses content types, which are the types of files that the Web server returns to a Web browser. All of these types are modeled on the MIME Internet Mail protocol, where the Web server tells the Web browser what kind of file it has, whether it is an HTML document, a GIF format image, a sound file, or a standalone application. Most Web browsers have a series of configurable helper applications that tell the browser how to handle the various types of content that the Web server sends over.
The HTTP communication mechanism is that during a complete HTTP communication, the following 7 steps will be completed between the Web browser and the Web server:
(1) establishing a TCP connection
before HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done via TCP, which works with the IP protocol to build the Internet, known as the TCP/IP protocol family, so the internet is also known as tcp/ IP network. HTTP is a higher level of application-level protocol than TCP, according to the rules, only the lower layer protocol is established before it can be a more protocol connection, so the first to establish a TCP connection, the port number of the general TCP connection is 80
(2) Web browser sends request command to Web server
Once a TCP connection is established, the Web browser sends a request command to the Web server
Example: get/sample/hello.jsp http/1.1
(3) Web browser sends request header information
after the browser sends its request command, it also sends some other information to the Web server in the form of header information, and then the browser sends a blank line to notify the server that it has ended sending the header information.
(4) Web server answer
After the client makes a request to the server, the server sends a reply back to the client,
http/1.1 OK
The first part of the answer is the version number of the protocol and the Answer status code
(5) The Web server sends the answer header information
Just as the client sends information about itself along with the request, the server also sends the user with the answer about its own data and the requested document.
(6) The Web server sends data to the browser
After the Web server sends a header message to the browser, it sends a blank line to indicate that the header information is sent to the end, and then it sends the actual data requested by the user in the format described by the Content-type reply header information
(7) The Web server shuts down the TCP connection
In general, once the Web server sends the request data to the browser, it closes the TCP connection and then if the browser or server joins this line of code in its header information
Connection:keep-alive
The TCP connection remains open after it is sent, so the browser can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.
    
HTTP request Format
When the browser makes a request to the Web server, it passes a block of data to the server, which is the request information, and the HTTP request information consists of 3 parts:
1. Request method URI Protocol/version
2. Requests header (Request header)

3. Request Body

HTTP Request message Format:

Get/web/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,en; q=0.8

Q is the weight factor, the greater the range 0 =< q <= 1,q value, the more the request tends to get its ";" The previous type represents the content,

If the Q value is not specified, the default is 1, and if assigned a value of 0, it is used to alert the server to which content types are not accepted by the browser

      request header and request body is a blank line, this line is very important, it means that the request header has ended, followed by the request body. The request body can contain query string information submitted by the customer:
      username=jinqiao&password=1234
      In the HTTP request for the example above, the body of the request has only one line of content. Of course, in real-world applications, the HTTP request body can contain more content.
HTTP request method I only discuss get method and post method here
      (1) Get method
      Get method is the default HTTP request method, We use the Get method to submit the form data on a daily basis, but the form data submitted with the Get method is simply encoded, and it is sent to the Web server as part of the URL, so there is a security risk if you use the Get method to submit the form data. For example
Http://127.0.0.1:8888/index.html? Name=lip
from the URL request above, it is easy to identify what the form submitted. (? Content) Additionally, the amount of data submitted is not too large because the data submitted by the Get method is part of the URL request
      (2) Post method
      Post Method is an alternative to the Get method, which is primarily to submit form data to the Web server, especially large batches of data. The Post method overcomes some of the drawbacks of the Get method. When submitting form data through the Post method, the data is not sent as part of the URL request but as standard data to the Web server, which overcomes the drawback that the information in the Get method is not confidential and the amount of data is too small. Therefore, for security reasons and respect for user privacy, the Post method is usually used for form submission.

HTTP replies are similar to HTTP requests, and HTTP responses are made up of 3 parts, namely:
(1) Protocol status version Code description
(2) Response Head (Response header)
(3) Response body
The following is an example of an HTTP response:

http/1.1 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

<title>HTTP Response Example <title>

<body>

Hello http!

</body>

The Protocol status code describes the first line of the HTTP response similar to the first line of the HTTP request, which indicates that the protocol used by the HTTP1.1 server has successfully processed the client-issued request (200 indicates success):
http/1.1 OK
The response header (Response header) also contains many useful information, such as server type, datetime, content type, and length, as well as the request header.


HTTP Answer code
An HTTP answer code, also known as a status code, reflects the state of the Web server processing HTTP requests. The HTTP answer code consists of 3 digits, with the first number defining the type of the answer code:

1xx-Information Class (information), which indicates receipt of a Web browser request, is being further processed

2xx-Success Class (successful), which indicates that user requests are received correctly, understood and processed for example: OK

The 3xx-redirect Class (redirection) indicates that the request was unsuccessful and the customer must take further action.

4xx-Client error, which indicates that the client submitted a request with an error such as: 404 Not Found means that the document referenced in the request does not exist.

5xx-Server error indicates that the server was unable to complete the processing of the request: 500


HTTP Server Development (Java)--http requests

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.