Correctly Setting HttpClient BaseAddress
[.NET, C#, HttpClient]
To make HTTP requests in C# (or any other .NET language), you use the HttpClient class.
For example, if I wanted to retrieve the text of the About page of this blog, as well as the Archives, I would do it as follows:
var client = new HttpClient();
// Get the about page
var about = await client.GetStringAsync("https://www.conradakunga.com/blog/about/");
// Get the archive page
var archive = await client.GetStringAsync("https://www.conradakunga.com/blog/archives/");
If you look closely, you will see that I am always repeating the same bit - https://www.conradakunga.com/blog/
Is it possible to avoid this repetition? Absolutely.
You do so by setting the BaseAddress property of the HttpClient
var client = new HttpClient()
{
// Set the base address URI
BaseAddress = new Uri("https://www.conradakunga.com/blog/")
};
// Get the about page
var about = await client.GetStringAsync("about");
// Get the archive page
var archive = await client.GetStringAsync("archives");
Of note here is that the BaseAddress
is a URI, and not a string.
Also, you must have the trailing slash, /
at the end of the BaseAddress
.
In other words, the following will not work
var client = new HttpClient()
{
// Set the base address URI - this will fail
BaseAddress = new Uri("https://www.conradakunga.com/blog")
};
You must also NOT have a leading slash, /
at the start of the request.
In other words, the following will not work:
var client = new HttpClient()
{
// Set the base address URI
BaseAddress = new Uri("https://www.conradakunga.com/blog/")
};
// Get the about page - this will fail!
var about = await client.GetStringAsync("/about");
This behaviour is consistent with the RFC for Uniform Resource Identifier (URI): Generic Syntax
In summary, you must place a trailing slash at the end of the BaseAddress but must NOT have a leading slash at the beginning of the request.
Happy hacking!