[JAVA] HTTP protocol learning notes

Keywords: encoding Attribute Java Web Server

1. introduction

HyperText Transfer Protocol (HTTP) is a standard (TCP) for client (user) and server (website) requests and responses. It is an application layer protocol based on TCP/IP protocol.

Request response mode

Stateless protocol

  1. Do not save the communication state between the request and the response, that is, do not perform persistent processing
  2. Although HTTP/1.1 is a stateless protocol, in order to achieve the desired function of keeping state, Cookie technology is introduced

Connectionless

  1. Limit processing to one request per connection. After the server finishes processing the customer's request and receives the customer's response, it disconnects

2. Request method

2.1 eight methods

The HTTP/1.1 protocol defines eight methods (also called "actions") to operate the specified resources in different ways.

Method name Explain
GET Requests to server specific resources are generally used to query information. The GET method requires the server to place the resource located by the URL in the data part of the response message, and the return to the client generally does not contain the "request content" part, and the request data is represented in the request line in the form of address
POST Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body.
HEAD Ask the server for a response consistent with the GET request, but the response body will not be returned. This method can GET the meta information contained in the response header without transferring the whole response content.
OPTIONS Returns the HTTP request method supported by the server for a specific resource, or tests the functionality of the server by sending a '*' request to the web server
PUT Upload the latest content to the specified resource location
DELETE Request server to delete the resource identified by request URL
TRACE Echo requests received by the server, mainly for testing or diagnosis
CONNECT Reserved in HTTP/1.1 protocol for proxy server that can change connection to pipeline mode

2.2 difference between get and POST

How to carry data
get request put data in url address
post request to put data in message body
Capacity of carrying data
Data submitted in GET mode can only have a maximum of 1024 bytes
POST does not have this limitation. The theory should be the available memory size of the client

3. Message structure

3.1 request message

A request message is shown below.


The format of the request message is shown in the figure below.

The request message can be roughly divided into three parts:

  1. Request line, which describes the client's request mode, the name of the request resource, and the version number of the HTTP protocol. For example: GET/BOOK/JAVA.HTML HTTP/1.1.
  2. Request Headers mainly contain some attribute information of this message, including Host, data type supported by client and Accept charset.
  3. According to the content type attribute in the request header, the content format in the Body is also different.

The common attributes of the request header are shown in the following table

Attribute name Explain
Accept Used to tell the server which data types the client supports (for example: Accept:text/html,image / *)
Accept-Charset Used to tell the server the encoding format adopted by the client
Accept-Encoding Used to tell the server the data encoding supported by the client
Accept-Language Language types supported by browsers
Host The host name that the client wants to access through this server
If-Modified-Since Through this header, the client tells the server the cache time of the resource
Referer Through this header, the client tells the server from which resource it (client) accesses the server (anti-theft chain)
User-Agent Through this header, the client tells the server the software environment of the client (operating system, browser version, etc.)
Cookie The client sends the Cookie information to the server through this header
Connection Tell the server whether to keep the connection after the request is completed
Date Tell the server the time of the current request
Transfer-Encoding Tell the receiver what encoding method is used to ensure the reliable transmission of the message

When the entity content in the request message is not empty, the request header contains the following attribute information.

Attribute name Explain
Content-Type Media type of entity body sent to the receiver (native form format, json format, multi file upload format, etc.)
Content-Length Length of entity body
Content-Language Describe the natural language of the resource. If it is not set, the entity content will be provided to all languages for reading
Content-Encoding The entity header is used as the modifier of the media type. Its value indicates the encoding of the additional content that has been applied to the entity body. Therefore, to obtain the media type referenced in the content type header field, the corresponding decoding mechanism must be adopted.
Last-Modified The entity header is used to indicate the last modified date and time of the resource
Expires The entity header gives the date and time when the response expires

3.2 response message

A reply message is shown below.

Format of response message.

The response message is also divided into three parts

  1. Status line, mainly including protocol version, status code and other information
  2. Response header, some attribute information, used to describe the message returned by the server
  3. The response body can have different formats according to the media type in the response header.

The common attributes in the response header are shown in the following table.

Attribute name Explain
Allow Which request methods are supported by the server (such as GET, POST, etc.)
Content-Type Media type of entity body sent to recipient
Content-Length Length of entity body
Content-Language Describe the natural language of the resource. If it is not set, the entity content will be provided to all languages for reading
Content-Encoding The entity header is used as the modifier of the media type. Its value indicates the encoding of the additional content that has been applied to the entity body. Therefore, to obtain the media type referenced in the content type header field, the corresponding decoding mechanism must be adopted.

4. Implementation of HTTP request in Java

4.1 JAVA native code implementation

GET request

public String getRequest(String requestUrl){
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// Return result string
        try {
            // Create remote url connection object
            URL url = new URL(requestUrl);
            // Open a connection through the remote url connection object, and forcibly convert to the httpURLConnection class
            connection = (HttpURLConnection) url.openConnection();
            // Set connection method: get
            connection.setRequestMethod("GET");
            // Set the timeout to connect to the host server: 15000 MS
            connection.setConnectTimeout(15000);
            // Set the data time to read remote return: 60000 MS
            connection.setReadTimeout(60000);
            // Send request
            connection.connect();
            // Get the input stream through the connection connection
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // Encapsulates the input stream is and specifies the character set
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // Storing data
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (Exception e) {
            log.error("Wrong:", e);
        } finally {
            // close resource
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// Close remote connection
        }
        return result;
    }

POST request

public String postRequest(String requestUrl, String param){
        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(requestUrl);
            // Open connection through remote url connection object
            connection = (HttpURLConnection) url.openConnection();
            // Set connection request method
            connection.setRequestMethod("POST");
            // Set the connection host server timeout: 15000 MS
            connection.setConnectTimeout(15000);
            // Set read host server return data timeout: 60000 MS
            connection.setReadTimeout(60000);

            // The default value is: false, which needs to be set to true when transmitting / writing data to the remote server
            connection.setDoOutput(true);
            // The default value is: true. When reading data to remote service, it is set to true. This parameter is optional
            connection.setDoInput(true);
            // Format the incoming parameter: the request parameter should be in the form of name1 = value1 & Name2 = Value2.
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // Setting authentication information: authorization: borer da3efcbf-0845-4fe3-8aba-ee040be542c0
            connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // Get an output stream by connecting objects
            os = connection.getOutputStream();
            // Write / transmit parameters through output stream object, which is written out by byte array
            os.write(param.getBytes());
            // Get an input stream through the connection object and read it to the remote
            if (connection.getResponseCode() == 200) {

                is = connection.getInputStream();
                // Wrap the input stream object: charset is set according to the requirements of the working project team
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuffer sbf = new StringBuffer();
                String temp = null;
                // Loop through row by row to read data
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // close resource
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // Disconnect from remote address url
            connection.disconnect();
        }
        return result;
    }
19 original articles published, praised 17, visited 60000+
Private letter follow

Posted by luked23 on Sun, 09 Feb 2020 23:50:03 -0800