C# tutorials > Input/Output (I/O) and Networking > .NET Networking > Making HTTP requests with `HttpClient`
Making HTTP requests with `HttpClient`
This tutorial demonstrates how to make HTTP requests using the `HttpClient` class in C#. `HttpClient` is a fundamental class in the .NET framework for sending HTTP requests and receiving HTTP responses from a web server. We'll cover basic GET and POST requests with examples and explanations.
Basic GET Request
This code snippet demonstrates a simple GET request. First, a new `HttpClient` instance is created within a `using` statement to ensure proper disposal of resources. `client.GetAsync()` sends a GET request to the specified URL. `response.EnsureSuccessStatusCode()` throws an exception if the response status code indicates an error (e.g., 404, 500). Finally, `response.Content.ReadAsStringAsync()` reads the content of the response as a string and prints it to the console. The `try-catch` block handles potential `HttpRequestException` exceptions, providing robust error handling.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class HttpClientExample
{
public static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
response.EnsureSuccessStatusCode(); // Throw exception if status code is not 200-299
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"Exception Caught! Message: {e.Message}");
}
}
}
}
Basic POST Request
This snippet illustrates a basic POST request. Similar to the GET example, an `HttpClient` instance is created. Data to be sent is serialized into a JSON string using Newtonsoft.Json (install the Newtonsoft.Json NuGet package). A `StringContent` object is created, containing the JSON data, encoding, and content type (`application/json`). `client.PostAsync()` sends the POST request with the specified URL and content. Error handling and response processing are the same as in the GET example.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
public class HttpClientExample
{
public static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
// Define the data to be sent in the POST request
var postData = new { userId = 1, title = "Sample Post", completed = false };
string json = JsonConvert.SerializeObject(postData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
// Send the POST request
HttpResponseMessage response = await client.PostAsync("https://jsonplaceholder.typicode.com/todos", content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"Exception Caught! Message: {e.Message}");
}
}
}
}
Concepts Behind the Snippet
Real-Life Use Case Section
Making HTTP requests with `HttpClient` is used extensively in various real-world scenarios:
Best Practices
Interview Tip
Be prepared to discuss the benefits of using `HttpClient` asynchronously, the importance of proper resource disposal, and common error handling techniques. Also, understand the use of `HttpClientFactory` for managing `HttpClient` instances, especially in high-volume scenarios.
When to Use Them
Use `HttpClient` when you need to communicate with web services over HTTP, whether it's retrieving data, sending data, or interacting with APIs. It's the primary class for making HTTP requests in .NET applications.
Memory Footprint
Creating multiple `HttpClient` instances can lead to socket exhaustion. Each `HttpClient` instance creates its own underlying `HttpClientHandler`, which can consume significant resources. Therefore, reuse `HttpClient` instances whenever possible or use `HttpClientFactory` to manage the lifecycle of `HttpClient` instances.
Alternatives
Pros
Cons
FAQ
-
How do I handle different HTTP methods (PUT, DELETE, PATCH)?
Use the corresponding methods on the `HttpClient` instance: `PutAsync`, `DeleteAsync`, and `PatchAsync`. They work similarly to `GetAsync` and `PostAsync`. -
How do I set request headers?
Use the `client.DefaultRequestHeaders` property to set default headers for all requests, or create an `HttpRequestMessage` and set headers on the `Request` property before sending the request. -
How do I handle different content types?
Set the `Content-Type` header in the request and serialize the data accordingly. For example, use `application/json` for JSON data, `application/xml` for XML data, and `multipart/form-data` for file uploads. -
How do I handle SSL/TLS certificates?
You can configure the `HttpClientHandler` to specify custom certificate validation logic. This is often needed when connecting to servers with self-signed certificates or when you need to perform client certificate authentication.