Goweb HTTP protocol

Time:2020-3-10

Link to another blog about http
It’s a bit slow on the first visit~~~

HTTP protocol

Because we have to know something about HTTP to write web applications, we will introduce http

HTTP protocol introduction

HTTP hypertext transfer protocol is an application layer protocol
Object oriented protocol is suitable for distributed hypermedia information system because of its simple and fast way. It is at 1990
After several years of use and development, it has been continuously improved and expanded. It’s a browser that specifies
The rules of communication with the world wide web server, the data transfer protocol for transmitting World Wide Web documents through the Internet.
The content transmitted when the client and the server communicate is called message
HTTP is a communication rule, which specifies the message format sent by the client to the server
The format of the message sent by the server to the client is determined. In fact, we need to learn these two kinds of messages. Client send
The one sent to the server is called “request message”, and the one sent to the client by the server is called “response message”.

The development of HTTP protocol

The predecessor of hypertext transfer protocol is Xanadu project. The concept of hypertext is Ted
Nelson) in the 1960s. After entering Harvard University, Nelson has been working on hypertext protocol and the project
But he has never published information. In 1989, Tim Berners Lee was in CERN (Europe
European Organization for Nuclear Research
At that time, we developed a set of programs, which laid the foundation of the world wide web. 1990
In December, hypertext was first launched at CERN. In the summer of 1991, after telnet and other protocols, hypertext transfer association
It has become a part of many Internet protocols.
At that time, telnet protocol solved the problem of one-to-one control communication between one computer and another
Requirement. The mail protocol solves the communication requirement that a sender sends information to a small number of people. File transfer protocol solution
The communication requirements of one computer to obtain documents in batch from another computer, but it does not have the ability to obtain documents at the same time
The function of displaying or processing files at the same time. News transmission protocol solves the communication of one to many news broadcast
Requirement. The communication requirement of hypertext is to acquire and display on one computer and store in multiple computers
Text, data, pictures and other types of files; it consists of two parts: Hypertext Transfer Protocol and hypertext mark
Remember the language (HTML). The birth of HTTP, HTML and browser has brought a leap to the popularity of the Internet.

Session mode of HTTP protocol

There are four steps in the communication process between browser and server: build, send, return, close

The connection process between browser and web server is short, and only one request and response is processed each time. For each
For page access, a separate connection should be established between the browser and the web server.

All communication between browser and web server is completely separate request and response pairs.

##Differences between HTTP 1.0 and HTTP 1.1

In HTTP version 1.0, when the browser requests a web page with pictures, it will contact the server due to downloading pictures
A new connection is opened between the two; however, in HTTP version 1.1, the browser is allowed to get all the corresponding information of the current request
After resources are disconnected, the efficiency is improved. HTTP 1.1 is the most widely used version at present, while the latest version is HTTP 2.0
Called http / 2. HTTP 2.0 will only be used for HTTPS: / / URLs on the open Internet. HTTPS, or SSL (secure
HTTP over socket layer is actually HTTP over SSL / TLS connection
Signal communication.
Note: SSL was originally developed by Netscape, and then by IETF (Internet Engineering Task Force,
Internet Engineering Task Force) took over and renamed it TLS (Transport Layer Security
Discussion)

Request message

  • Message format

    • Request first line (request line);
    • Request header information (request header);
    • Blank line;
    • Request body;
  • Get request
GET /Hello/index.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64;
Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;
Media Center PC 6.0; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Host: localhost:8080
Connection: Keep-Alive
Cookie: JSESSIONID=C55836CDA892D9124C03CF8FE8311B15

Get request has no request body, post request has request body

Get / hello / index.jsp http / 1.1: get request, request server path is
Hello / index.jsp, protocol 1.1;

  • Host: localhost: the requested host name is localhost;

  • User-Agent: Mozilla/4.0 (compatible; MSIE 8.0… : browser and OS related

Information. Some websites will display the user’s system version and browser version information by obtaining the user-
From agent header information;

  • Accept: /: tells the server the type of document that the current client can receive,/It means
    Everything can be received;

  • Accept language: zh CN: the language supported by the current client can be found in the browser’s tools

 find language related information in the options;

  • Accept encoding: gzip, deflate: supported compression formats. When data is transferred over the network,
    Maybe the server will compress the data before sending it;

  • Connection: keep alive: the link mode supported by the client. Keep the link for a period of time
    3000 ms;

  • Cookie: jsessionid = 369766fdf6220f78034333c0b2de36d98: because it is not
    First access to this address, so the cookie sent from the last server response will be sent in the request
    Send it in the request.

  • Post request
    The post request requires that the method property of the form tag be set to post

POST /Hello/target.html HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml,
image/gif, image/pjpeg, application/x-ms-xbap, */*
Referer: http://localhost:8080/Hello/
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64;
Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;
Media Center PC 6.0; .NET4.0C; .NET4.0E)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 14
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=774DA38C1B78AE288610D77621590345

Referer: http://localhost: 8080/hello/index.jsp: which page does the request come from, for example, when you are in 100
If you click the link here, then the referer: http://www.baidu.com; if you are in the browser’s place
The address entered directly in the address field, then there is no referer request header;

  • Content type: application / x-www-form-urlencoded: data type of form, description
    The URL format will be used to encode the data; the URL encoded data is prefixed with “%”, followed by two bits of 16
    For example, the URL encoding of UTF-8 is used as “E4% BC% A0% E6% 99% Ba”;

  • Content length: 13: the length of the request body, which represents 13 bytes.

  • Keyword = Hello: request body content! Hello is the data entered in the form, and keyword is the form
    The name of the field.

response message

  • Message format
    • Response first line (response line);
    • Response header information (echoing);
    • Blank line;
    • Response body;
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 274
Date: Tue, 07 Apr 2015 10:08:26 GMT




Insert title here


Hello
  • Http / 1.1200 OK: the response protocol is HTTP 1.1, and the status code is 200, indicating that the request is successful;
  • Server: Apache Coyote / 1.1: version information of the server;

  • Content type: text / HTML; charset = UTF-8: the code used by the response body is UTF-8;

  • Content length: 274: the response body is 274 bytes;

  • Date: Tue, 07 APR 2015 10:08:26 GMT: response time, which may be 8 hours
    District difference;

Response status code

  • The status code is used to tell the HTTP client whether the HTTP server has generated the expected response. HTTP/1.1
    In the protocol, five kinds of state codes are defined. The state code is composed of three digits. The first digit defines the response class
    other

    • 1XX prompt message – indicates that the request has been received successfully, continue processing

    • 2XX success – indicates that the request has been successfully received, understood, accepted

    • 3xx redirection – further processing is required to complete the request

    • 4xx client error – the request has a syntax error or the request cannot be implemented

    • 5xx server side error – the server failed to implement a legitimate request

  • The response code is very important to the browser. It tells the browser the result of the response. The common status codes are:
    • 200: if the request is successful, the browser will display the response content (usually HTML) in the browser;

    • 404: the requested resource was not found, indicating that the client requested the non-existent resource in error;

    • 500: the requested resource was found, but an error occurred inside the server;

    • 302: redirection. When the response code is 302, the server requires the browser to send another request,
      The server will send a response header location, which specifies the URL address of the new request;