This example uses the HttpClient class which sends and receives HTTP responses from a resource identified by a URI.
Calling REST API using .NET :
//Add Basic Authorization Headers
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{<username>}:{<password>}")));
//Add Accept Header
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/pdf"));
//Send GET request to Client Server
HttpResponseMessage response = client.GetAsync(client.BaseAddress).Result;
//Render out a PDF file
using (var stream = await response.Content.ReadAsStreamAsync())
{
var fileinfo = new FileInfo("test1.pdf");
using (var fileStream = fileinfo.OpenWrite())
{
await stream.CopyToAsync(fileStream);
}
}
This example uses .NET v5.0.301
Resources
Basic Authorization Header: c# - Setting Authorization Header of HttpClient - Stack Overflow
Setting up Accept Header & Sending a GET request: .net - How do I make calls to a REST API using C#? - Stack Overflow
Render out a PDF file: download file from url asp net web api c# Code Example
Source Code can be found here:
Program.zip (869 Bytes)