Trabalhando com Rede e API HTTP em C# (.NET): Guia Detalhado
What is it and why? (a real-life analogy)
Imagine you're sitting in a cafe and want to order a pizza. You don't go to the kitchen yourself, you don't get into the oven, and you don't touch the dough. Instead, you call the waiter, tell him your order, he goes to the kitchen, and after a while returns with a ready pizza. You don't even know how it was prepared — you only care about the result.
Working with the network and HTTP API in programming works exactly the same way. Your program (client) sends a request to a remote server (like an order to a waiter), the server processes this request and returns a response (like a ready dish). All of this happens according to rules called HTTP (HyperText Transfer Protocol).
Why is this needed? Imagine you're writing a weather forecast application. You wouldn't install weather stations all over the world yourself, would you? Instead, you contact a weather service (for example, OpenWeatherMap), send a request: "What's the weather in Moscow?", and get a ready response with temperature, humidity, and wind. Or, for example, you're writing an online store — you need to get a list of products, add new orders, check delivery status. All of this is done via HTTP API.
Important: API (Application Programming Interface) is simply a set of rules by which programs communicate with each other. HTTP API is an API that works over the HTTP protocol. When you hear "REST API" or "Web API" — these are all varieties of HTTP API.
In the modern world, almost no serious application can do without working with the network. Whether it's a mobile application, a website, a desktop program, or a game — they all constantly exchange data with servers. And C# (.NET) provides powerful and convenient tools for this.
Theory: how it works from the inside
Let's look "under the hood" and understand what happens when your program sends an HTTP request.
Basic HTTP concepts
HTTP is a text protocol. This means that requests and responses are simply strings of text that are transmitted over the network. Each HTTP message has three main parts:
- Start line — contains the request method (GET, POST, PUT, DELETE, etc.), the path to the resource, and the protocol version.
- Headers — meta-information about the request or response (data type, message length, cookies, authorization, etc.).
- Body — the data itself (optional, GET requests have no body).
HTTP methods (verbs)
The HTTP method indicates what action we want to perform. The most popular ones:
| Method | What it does | Real-life analogy |
|---|---|---|
| GET | Get data | Look at the menu in a cafe |
| POST | Create a new resource | Place an order |
| PUT | Completely replace a resource | Replace the entire dish with a new one |
| PATCH | Partially modify a resource | Ask to add cheese to the pizza |
| DELETE | Delete a resource | Cancel an order |
Status codes
The server always responds with a numeric code that indicates the result of the operation. Codes are grouped by hundreds:
| Range | Category | Examples |
|---|---|---|
| 1xx | Informational | 100 Continue |
| 2xx | Success | 200 OK, 201 Created |
| 3xx | Redirection | 301 Moved Permanently |
| 4xx | Client error | 400 Bad Request, 404 Not Found, 401 Unauthorized |
| 5xx | Server error | 500 Internal Server Error, 503 Service Unavailable |
URL — resource address
URL (Uniform Resource Locator) is the address by which we access a resource. It consists of several parts:
https://api.weather.com/v2/weather?city=Moscow&units=metric
|______| |______________| |___| |________________________|
protocol domain path query parameters
- Protocol — usually http or https (secure).
- Domain — server address (can be an IP address).
- Path — points to a specific resource on the server.
- Query parameters (Query String) — start with
?, separated by&. Used to pass additional data in GET requests.
Data formats: JSON and XML
When the server returns data, it must be in some format that is understandable to both humans and programs. The most popular today is JSON (JavaScript Object Notation). It looks like this:
{
"city": "Moscow",
"temperature": 22.5,
"humidity": 6