Fundamentals 12 min read

Mastering HTTP: From Requests to Responses and Protocol Differences

This comprehensive guide reviews the essential elements of HTTP requests and responses, explains version differences, clarifies the distinctions between HTTP, Socket, and TCP, and provides practical iOS implementation details for developers seeking a deeper understanding of web communication.

21CTO
21CTO
21CTO
Mastering HTTP: From Requests to Responses and Protocol Differences

HTTP, short for Hypertext Transfer Protocol, defines the rules for data exchange between clients and servers and can transmit any type of data, not just text.

HTTP transmission diagram
HTTP transmission diagram

HTTP Request and Response Content

A typical HTTP request includes a request line, headers, and an optional body; a response includes a status line, headers, and an entity body.

1. Request Line

The request line contains the method (e.g., GET, POST), the URI (the path part of the URL), and the HTTP version (commonly 1.1 or 2.0).

2. Request Headers

Headers convey additional information such as Host, Accept, Content-Type, Accept-Language, Accept-Encoding, User-Agent, Connection, Content-Length, and Cookie.

HTTP request header example
HTTP request header example
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [cookieJar cookies]) { NSLog(@"%@", cookie); }

3. Request Body

The body carries the actual data sent to the server, such as form fields in a POST request or binary data in a multipart upload.

4. Response Status Line

The status line includes the HTTP version, a numeric status code, and a textual reason phrase (e.g., "200 OK").

HTTP/1.1 200 OK

Status codes are grouped by their first digit: 1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error.

5. Response Headers and Body

Response headers are similar to request headers but may contain different fields; the body holds the returned content.

HTTP Version Overview

Before HTTP/1.1, connections were short‑lived, lacked headers, and were synchronous. HTTP/1.1 introduced persistent connections, request/response headers, and asynchronous request handling. HTTP/2.0 builds on 1.1, requires HTTPS, and adds further performance improvements.

Asynchronous request illustration
Asynchronous request illustration

Differences Between HTTP, Socket, and TCP

TCP operates at the transport layer, providing reliable byte streams. Socket is an abstraction over TCP/UDP, offering a programming interface for network communication. HTTP is an application‑layer protocol that runs on top of TCP.

Protocol stack diagram
Protocol stack diagram

Key distinctions:

HTTP connections are typically short‑lived; socket connections (based on TCP) can remain open for long periods.

HTTP follows a request‑response model; sockets allow bidirectional, peer‑to‑peer communication.

Use HTTP when occasional requests suffice (e.g., fetching resources, uploading files). Use sockets for real‑time communication such as chat or push notifications.

In iOS, HTTP requests are commonly made with NSURLSession, NSURLConnection, or libraries like AFNetworking; socket communication often uses CocoaAsyncSocket.

The End

This review of HTTP concepts is suitable for beginners and experienced developers alike.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

iOSHTTPNetworkingprotocolSocket
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.