What is the HTTP

Hypertext Transfer Protocol (HTTP) is a simple request-response Protocol that usually runs on top of TCP. It specifies what messages the client might send to the server and what responses it might get. — From Baidu Encyclopedia

In plain English, this is a request-response protocol.

Request part

First, let’s take a look at chestnut 🌰 :

In the address box of your browser, type https://blog.csdn.net/m0_46278918 and press Enter.

You can see the request data:

The data has been formatted by the browser in this format:

GET /m0_46278918 HTTP/1.1 Host: blog.csdn.net Content-type: text/plain Content-Length: 3 ABCCopy the code

Ok, let’s start parsing:


Request:

GET/m0_46278918 HTTP1.1

  • GET: Request mode
  • /m0_46278918: Request path
  • HTTP1.1HTTP version:
Request method:
  • GET: Obtains resources
  • POST: Adds or modifies resources
  • PUT: Modifies resources
  • DELETE: deletes resources
Request path:

The server returns what the user needs based on the path.

The HTTP version:

By far the most popular version is 1.1, with 1.0 and 2.0 equally prevalent.


Header information:

Host: blog.csdn.net

Content-Type: text/plain

Content-Length: 3

Accept-encoding: gzip, accept-charset: UTF-8, etc.

Among them:

  • Host: domain name
  • Content-type: indicates the Content format
  • Content-length: indicates the Content Length
  • Accept: Indicates the data type accepted by the client. For example, text/ HTML, application/ JSON
  • Accept-encoding: Indicates the Encoding type accepted by the client. Such as: gzip
  • Accept-charset: character set accepted by the client. Such as UTF-8 and GBK

Content:

abc

em… On request content


At this point, the request data format basic description, but, I believe that we may have questions, request data format is like this, but in the code it is how to achieve?

Ok, the code is as follows:

    private static final String host = "blog.csdn.net";
    private static final int port = 80;
    private static final String content = "GET /m0_46278918  HTTP/1.1\n" +
            "Host: blog.csdn.net\n" +
            "Content-Type: text/plain\n\n";
    private static String responseContent = "";

    public static void main(String[] args) {
        try (Socket socket = new Socket(host, port);
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
             BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            writer.write(content);
            writer.flush();
            while ((responseContent = reader.readLine()) != null) {
                System.out.println(responseContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Copy the code

The logic of the code is also simple:

  • Establishing a Socket Connection
  • Write request data
  • The loop reads the response data and outputs it

Return result:

What does this show?

Explains the browser to HTTP internal request principle is not so complex, just the request data in accordance with a certain format and then sent to the server.

The server returns the data in a format that is the HTTP response.

The response part

// Status line HTTP/1.1 301 Moved Permanently // Header information Server: openResty Date: Tue, 16 Mar 2021 15:38:14 GMT Content-type: text/html Content-Length: 182 Connection: keep-alive Keep-Alive: timeout=20 Location: https://blog.csdn.net/m0_46278918 // contents < HTML >< head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>openresty</center> </body> </html>Copy the code

HTTP/1.1 301 Moved Permanently

  • HTTP / 1.1HTTP version:
  • 301Status code:
  • Moved Permanently: Status Description

Among them, the most important is the status code, which has the following types:

  • 1xx: Temporary message. The server receives the request and requires the requester to continue.
    • 100 (Continue to send)
    • 101(Switching protocol)
  • 2xx: The request succeeded.
    • 200 (successful)
    • 201(Created successfully)
  • 3xx: redirection. Further action is required to complete the request.
    • 301(Permanent Migration)
    • 302(Temporary Relocation)
    • 304(Contents unchanged)
  • 4xx: Client error. The request contained a syntax error or could not complete the request.
    • 400(Client request error)
    • 401(Authentication failed)
    • 403(Prohibited)
    • 404(contents not found).
  • 5xx: Server error. The server encountered an error while processing the request.
    • 500(Server internal error)