liveBook · Manning

HTTP

As explained in .NET in Action, Second Edition

HTTP is an application-layer protocol for transferring resources and exchanging messages between clients and servers.

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

How .NET in Action, Second Edition covers it

How deep it goes
Explains HttpClient retrieval, JSON deserialization, request failures, HTTPS redirection, certificates, and HSTS, but not protocol internals.
Examples
Demonstrates xkcd metadata retrieval, title search, HTTPS redirection, development certificates, and production HSTS configuration.
What it assumes
Assumes JSON parsing, serialization, streams, and basic ASP.NET Core application configuration.
What you can do after
Operate/configure HTTP clients and ASP.NET Core services that retrieve JSON, handle request failures, and enforce HTTPS.
How it connects
Connects HTTP with HttpClient, JSON serialization, HTTPS, certificates, HSTS, and ASP.NET Core.

Also explained in

Compare how 8 books explain HTTP →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.