HTTP is an application-layer protocol for transferring resources and exchanging messages between clients and servers. In .NET applications, it provides the runtime path from a client request to a remote response, often carrying JSON that can be deserialized into domain objects. section 4.3
A practical HTTP integration usually combines these operations:
- Create an HttpClient with a base address, request a resource with GetStreamAsync, and deserialize the response using JsonSerializer. section 4.3
- Map remote JSON names to C# properties with JsonPropertyName, and expose convenient derived values such as a parsed DateOnly. section 4.3
- Handle failed requests by recognizing HttpRequestException directly or inside an AggregateException, then returning an unavailable result. section 4.3
- Protect traffic with HTTPS, which requires a certificate; redirect HTTP requests or terminate HTTPS at a production proxy. section 9.3
Searching xkcd comics
A console application retrieves the latest comic number, requests comic metadata in descending order, compares titles case-insensitively, and prints the matching number and publication date. Because the API has no search operation, the client performs the search through repeated HTTP requests. section 4.3
Listing 4.30 Retrieving comic metadata via HTTP request and deserializing
private static HttpClient client = new HttpClient() #1
{ BaseAddress = new Uri("https://xkcd.com") }; #2
public static Comic? GetComic(int number) #3
{
try
{
var path = number == 0 ? "info.0.json" #4
: $"{number}/info.0.json";
var stream = client.GetStreamAsync(path).Result; #5
return JsonSerializer.Deserialize<Comic>(stream); #6
}
catch (AggregateException e) #7
when (e.InnerException is HttpRequestException) #8
{
return null;
}
catch (HttpRequestException) #8
{
return null;
}
}
Inspect the focused C# implementation that builds a resource path, retrieves JSON over HTTP, deserializes it, and handles request failures. section 4.3
Listing 9.3 Enabling HSTS in production
var app = builder.Build();
if (app.Environment.IsProduction()) #1
{
app.UseHsts(); #2
}
Inspect the production-only HSTS configuration that instructs browsers to use HTTPS for the service. section 9.3







