This is a creation in Article, where the information may have evolved or changed.
Recently bought a book, "http authoritative guide", read the first chapter, see the book said through Telnet analog HTTP request, I learned that I have not learned computer network.
Today, the examination of the designated teaching materials "computer network", which also visited the Tsinghua University home page for an example of detailed communication process:
- The URL of the browser analysis link to the page.
- The browser resolves the IP address of the www.tsinghua.edu.cn to the DNS request.
- DNS resolution of the domain Name system the IP address of the Tsinghua University server is 166.111.4.100.
- The browser establishes a TCP connection with the server (the server-side IP address is 166.111.4.100 and the port is 80).
- Browser issue fetch File command: GET/CHN/YXSZ/INDEX.HTMA
- The server www.tsinghua.edu.cn the response and sends the file index.htm to the browser.
- Releases the TCP connection.
- The browser displays all the text in the "Department of Tsinghua University Settings" file index.htm.
The HTTP protocol is to establish a TCP connection with the specified IP and port, and then send the request command, header, and data over the connection. The data that the server responds to is also available through this request.
The examination of the research and even this does not understand the hurt AH.
Here I mostly want to simulate an HTTP request based on a TCP connection. Tests whether HTTP header information can be sent, which is simulated by the User-agent field. Also send the body information to test body.
1. Telnet Impersonation Request
$ telnet blog.cyeam.com 80GET / HTTP/1.0
This command can establish a connection, after it is established, enter the request command, the method is get, the path is the root path \
, and the port is 80. Then enter the two return, you can send the request. The response status code, response header, and body are returned here. Once the response is returned, the Telnet request will also be closed. Post send can refer to HTTP Post via Telnet-burak ' s logs
2. Postman Simulation Request
Postman is a chrome plugin that is powerful enough to emulate HTTP requests and view the full contents of requests and responses. I also usually use its preview function, so I can see all the request data.
3. Beego Emulation Server
You can also use the server on the public network to test, but there is no way to view the server side received the details of the request, with Beego server to test (server or beego the most familiar ... )。
Beego get user-agent through this.Ctx.Request.UserAgent()
implementation. Get the body content through string(this.Ctx.Input.RequestBody)
implementation, also need to add in the configuration file copyrequestbody = true
, the default is taken to the body is empty. Validation of Beego server functions can be simulated by postman.
4. Post request for HTTP based on TCP sockets
View full request and Telnet using Postman's preview, and find that the command, header, and body of the HTTP request are differentiated by newline characters \n
. Postman caught the following:
POST HTTP/1.1Host: localhost:8080Cache-Control: no-cachePostman-Token: 649dbec6-4f13-7064-67f0-11461281ceb2This is body
You can conclude that HTTP commands, headers, and body are distinguished by line breaks. The first line is the request command, followed by the HTTP header, they are separated by a newline character, and the header is also differentiated by a newline character. After two line breaks, it is the body information.
Send a request via a large Golang:
conn.Write([]byte("POST / HTTP/1.0\nUser-Agent: Cyeam\nContent-Length: 4\n\nBody\r\n\r\n"))
Note that if you want to pass in the body content, you must add the length information in the header, Content-Length: 4
or the server will still not be able to obtain, anyway Beego is so, may be to get the body before you judge the length, if it is 0 does not read the body information. The full code draws on the content of the fifth chapter of Go language programming.
This article covers the complete source code, please test the client and test server.
Reference documents
- "1" http authoritative guide
- "2" "Computer network-Shehiren"
- "3" HTTP POST via Telnet-burak ' s logs
Original link: Based on TCP sockets, through the Golang analog HTTP request, reprint please specify the source!