Understanding HTTP Protocol: Basics, Request Process, and Server Response
This article provides a comprehensive overview of the HTTP protocol, covering its fundamental concepts, the complete request lifecycle—including DNS resolution, TCP handshake, request headers, methods, cookies, and tokens—and detailed server response structures with practical Spring Boot examples.
Introduction
The author frequently encounters remote service calls, domain configuration, and request forwarding in real projects and decides to share how HTTP concepts are applied in practice, beyond theoretical study.
1. HTTP Protocol
1.1 Basic Concepts
HTTP (Hyper Text Transfer Protocol) is an application‑layer protocol for transmitting text, images, audio, video, and other hypermedia data. It is stateless, meaning each request is independent, and forms the foundation of data communication on the World Wide Web.
HTTP defines how a web client requests a page and how a server responds, but it does not mandate a specific transport protocol; in practice it runs over TCP for reliable delivery.
1.2 Working Principle
A typical HTTP interaction involves the client establishing a TCP connection to the server (default port 80), the server returning a status line such as "HTTP/1.1 200 OK", and then sending the requested content.
Key steps of the HTTP workflow are:
Step 1: Establish a TCP/IP connection via a three‑way handshake.
Step 2: Client sends an HTTP request, e.g., POST /login.html HTTP/1.1.
Step 3: Client transmits request headers and body, ending with a blank line.
Step 4: Server responds, e.g., HTTP/1.1 200 OK.
Step 5: Server sends response headers followed by the response body according to the Content‑Type.
Step 6: Server closes the TCP connection unless Connection: keep‑alive is used.2. Request Process
2.1 Domain Name Resolution
The browser resolves the domain name to an IP address through the following steps:
Browser cache : Checks its own DNS cache (short‑lived, ~1 minute, up to 1000 entries).
Operating system cache : Queries the OS DNS cache (e.g., ipconfig /displaydns on Windows).
Hosts file : Reads the local hosts file (e.g., C:\Windows\System32\drivers\etc\hosts ).
DNS server : Sends a UDP request to the configured DNS server on port 53 for recursive resolution.
2.2 TCP Connection
Using the resolved IP and default port, the client performs the first two steps of the TCP three‑way handshake to establish a connection.
2.3 Sending HTTP Request
The third step of the TCP handshake completes, and the client sends the HTTP request.
2.4 Server Response
If the request reaches the server, the server must generate a response according to the HTTP protocol.
2.5 Response Content Example
An example of response headers when accessing the Maven Central repository:
2.6 Closing the Connection
Finally, the browser closes the TCP connection after rendering the received resources.
3. Client Request Details
3.1 Request Header
An HTTP request consists of a request line, headers, an optional body, and a blank line. Headers are key‑value pairs separated by a colon, as shown in a typical Postman capture.
Common headers include Authorization (authentication) and custom headers such as Tenant-Code .
Note: The HTTP specification does not dictate the encoding of POST bodies; the server determines the encoding from the Content‑Type header.
application/x-www-form-urlencoded : Form data encoded as key=value pairs with URL‑encoding.
multipart/form-data : Used for file uploads; data is split by boundaries.
application/json : Body contains a JSON string, typically sent via Ajax.
3.2 Request Methods
The four most common HTTP methods are GET, POST, PUT, and DELETE.
3.3 Cookies and Tokens
Because HTTP is stateless, mechanisms such as Session (server‑side) and Cookie (client‑side) are used to maintain state.
Sessions store user data on the server, while cookies are sent by the browser on subsequent requests to identify the user.
4. Server Response
4.1 Demo with Spring Boot
A simple Spring Boot controller demonstrates how a request is mapped and handled:
@RestController
@RequestMapping("/study")
public class StudyController {
@Resource
private StudyService studyService;
/**
* Add a new study record
* @param studyDTO
* @return success flag
*/
@PostMapping("/add")
public BaseResponse
addAwards(@RequestBody StudyDTO studyDTO) {
return ResultUtils.success(studyService.addStudy(studyDTO));
}
}Access it via https:// ip : port /study/add or locally at http://localhost:28089/initial/study/add .
4.2 Response Content
The HTTP response consists of three parts:
Status Line : Protocol version, status code, and reason phrase (e.g., HTTP/1.1 200 OK).
Headers : Key‑value pairs such as Content‑Type , Content‑Length , Location , Set‑Cookie , Cache‑Control , Server , etc.
Body : The actual payload, which may be HTML, JSON, plain text, images, XML, etc., as indicated by the Content‑Type header.
4.3 Common Status Codes
Typical HTTP response status codes include 200 (OK), 301 (Moved Permanently), 302 (Found), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), and others.
5. Conclusion
Understanding HTTP is essential for both front‑end and back‑end developers, regardless of background. Mastery of the protocol enables efficient, flexible, and reliable data transmission for modern web applications.
The author welcomes feedback and discussion in the comments.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.