Basic HttpClient usage tips in c#

Here is an example of how to add query parameters to http client request



string requestUrl = "https://example.com";

requestUrl = QueryHelpers.AddQueryString(requestUrl, new Dictionary<string, string>()
{
  	["param1"] = "param1val",
  	["param2"] = "param2val"
});

When using HttpClient with a base address, the base URL must end with '/'



HttpClient httpClient = new () {
	BaseAddress = new Uri("http://example.com/api/")
};
...

httpClient.GetFromString("resource/one");

Post a Comment

Previous Post Next Post