It is well known that communication between a web server and a client uses the HTTP protocol. HTTP is a standard for client and server requests and responses (TCP). Because the HTTP protocol is based on the TCP protocol, I will use Socket in JAVA to complete this simple web server. For more detailed HTTP information, you can consult the relevant information to learn about it.
Before writing the server, let’s take a look at the rules of communication between the browser and the server.
First of all, we use ServerSocket to simulate a server, access it through the browser, and view the content requested by the browser:
import java.io.BufferedWriter;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import org.junit.Test;/** * HTTP protocol test* * @author jianggujin * */public class HQHttpProtocolTest{ @Test public void server() throws Exception { ServerSocket serverSocket = new ServerSocket(80); Socket socket = serverSocket.accept(); InputStream stream = socket.getInputStream(); int r = -1; while ((r = stream.read()) != -1) { System.out.print((char) r); } }}Run it with junit and access it through the browser: http://127.0.0.1 , we can see that the browser's request content output on the console is as follows:
GET / HTTP/1.1Host: 127.0.0.1Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36Accept-Encoding: gzip,deflate,sdchAccept-Language: zh-CN,zh;q=0.8
In order to better analyze the request content, we write an HTML page to submit some data and view the request content again:
<!DOCTYPE html><html><head><meta charset="utf-8"><title>test</title></head><body><form method="post" action="http://127.0.0.1?test=123"><input type="text" name="name"/><input type="submit"/></form></body></html>
Enter bob in the input box, click the button to submit, and observe the console output:
POST /?test=123 HTTP/1.1Host: 127.0.0.1Connection: keep-aliveContent-Length: 8Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: nullUser-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36Content-Type: application/x-www-form-urlencodedAccept-Encoding: gzip,deflate,sdchAccept-Language: zh-CN,zh;q=0.8name=bob
Let's analyze the content of this request:
The first line: consists of three parts, separated by spaces in the middle, the first part is the request method (GET, POST), the second part is the request path and query parameters, and the third part is the HTTP protocol version (HTTP/1.1)
Line 2 to line 10: The header information of the request, the name and value of the request header is passed: Separate the eleventh line: empty line 12th line: Submitted form content, we can get the following conclusion: the first behavior of the request information is the request method, the request path, the query parameters, and the HTTP protocol version. After the /r/n line break, the request header information is immediately followed by the /r/n line break, the request header information is followed by a blank line after the request header information is completed, and the request data is immediately followed by a blank line. It should be noted that this is only simulated. As for complex file submissions, etc., it is not discussed here, and the request content format is slightly different.
At this point, we have already known the content requested by the client. Let’s take a look at the format of the server’s response data after receiving the request. We create a new Web project for testing and edit the content of the Html page as follows:
<!DOCTYPE html><html><head><meta charset="utf-8"><title>test</title></head><body>this is test page.</body></html>
Start the server, then write the client test code to obtain the server return data:
import java.io.BufferedWriter;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;import org.junit.Test;/** * HTTP protocol test* * @author jianggujin * */public class HQHttpProtocolTest{ public void server() throws Exception { ServerSocket serverSocket = new ServerSocket(80); Socket socket = serverSocket.accept(); InputStream stream = socket.getInputStream(); // BufferedInputStream inputStream = new BufferedInputStream(stream); int r = -1; while ((r = stream.read()) != -1) { System.out.print((char) r); } } @Test public void client() throws Exception { Socket socket = new Socket("127.0.0.1", 80); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())); writer.write("GET /Servlet/test.html HTTP/1.1/r/n"); writer.write("Host: 127.0.0.1/r/n"); writer.write("Connection: keep-alive/r/n"); writer.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8/r/n"); writer.write("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36/r/n"); writer.write("Accept-Encoding: gzip,deflate,sdch/r/n"); writer.write("Accept-Language: zh-CN,zh;q=0.8/r/n"); writer.write("/r/n"); writer.flush(); InputStream stream = socket.getInputStream(); int r = -1; while ((r = stream.read()) != -1) { System.out.print((char) r); } }}Running the program to get the server returns the following content:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1Accept-Ranges: bytesETag: W/"129-1456125361109"Last-Modified: Mon, 22 Feb 2016 07:16:01 GMTContent-Type: text/htmlContent-Length: 129Date: Mon, 22 Feb 2016 08:08:32 GMT<!DOCTYPE html><html><head><head><meta charset="utf-8"><title>test</title></head><body>this is test page.</body></html>
Similarly, let's analyze this return message:
The first line consists of three parts, separated by spaces in the middle, the first part is the HTTP protocol version (HTTP/1.1), the second part is the response status code, and the third part is the response status description of the second line to the seventh line, the response header information is passed between the response header name and the value: Separate the eighth line: the ninth line to the end of the response content: In summary, we can get the following conclusion: the first thing is the request information HTTP protocol version, the response status code, and the response status description, and the response header information is followed by the /r/n line break, and the response header information is passed by /r/n line break, and the response header information is followed by a blank line after the response header information is completed, and the response data is immediately followed by the response data. It should be noted that in addition to this response, there are actually other corresponding methods, such as chunk, which is not discussed here, and you can check the relevant information.
So far, we have analyzed the client's request content format and the corresponding content format of the server. This article ends here. I hope it will be helpful to everyone's learning.