Simple beauty jodd HTTP — in-depth understanding of the source code HTTP protocol
Jodd is an open source Java tool set, which contains some practical tool classes and small frameworks. Simple but powerful!
Jodd HTTP is a lightweight HTTP client. Now let's take a simple example to see how it is implemented from the source layer?
Build a get request
First review the format of HTTP request message:
The following figure shows the properties of a general request
Call get method to build HTTP request:
The method is as follows:
The set method is as follows:
According to the above method, the following parts are parsed according to the destination:
1. Method: http1 1. Seven request methods are supported: get, post, head, options, put, delete and tarce.
2. Protocol: http or HTTPS
3. Host: requested server address
4. Port: the requested server port
5. Path + query parameter, where the parameter is "?" At the beginning, use "&" to connect
Send request
First, get familiar with the format of HTTP response message:
The response header generally includes the following contents:
1. Open HTTPCONNECTION
Judge whether there is a connection. If there is no connection, create a new connection.
2. Create connection implementation
3. Create socket
According to different protocols, HTTP uses socketfactory to create sockets, and HTTPS uses sslsocketfactory to create sslsockets. Finally, the socket HTTPCONNECTION is used for packaging.
Socket HTTPCONNECTION inherits from HTTPCONNECTION and realizes the input-output stream connection of socket. Note: HTTPS needs to shake hands after creating sslsocket.
Open the output stream of connection to send information, and open the input stream of connection to accept the return information.
Sending process:
Write the data in the buffer to the output stream and send it.
Receive data and read message content:
Summary
From the above code, we can see that HTTP uses socket to establish a connection with destination, and then communicates through the connected output stream and input stream.
reference:
【1】 http://www.it165.net/admin/html/201403/2541.html
【2】 http://jodd.org/doc/http.html