I have called a Blackboard Learn APIs to download a file. The Url is
When I click on the link on the browser I saw that there are 2 more 302 Found before getting content rendered on the browser.
https://blackboard.ddns.net/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download Status code: 302
I tried on the Postman by sending Bearer token and request a GET request to call the method and it also received the content.
I have to write C# code to receive the content of the file but I don't receive file but receive 500 InternalServerError. I have been trying to fix the issue but no luck. Do you have any thoughts on this? Please help.
My code in C# console application is as below:
static void Main(string[] args)
{
//GET /learn/api/public/v1/courses/{courseId}/contents/{contentId}/attachments/{attachmentId}/download
string fileUrl = "/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://blackboard.ddns.net/");
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "avnPkfmWSnf2Y5aKf1497LahLD3eKuwn");
byte[] result = null;
HttpResponseMessage response = client.GetAsync(fileUrl).Result;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsByteArrayAsync().Result;
}
}
}
After reading a post How can I get System.Net.Http.HttpClient to not follow 302 redirects?, I tried to follow the 302 redirections and change the code as below but got the same 500 InternalServerError at the end.
static void Main(string[] args)
{
//GET /learn/api/public/v1/courses/{courseId}/contents/{contentId}/attachments/{attachmentId}/download
string fileUrl = "/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download";
HttpClientHandler clientHander = new HttpClientHandler();
clientHander.AllowAutoRedirect = false;
using (HttpClient client = new HttpClient(clientHander))
{
client.BaseAddress = new Uri("https://blackboard.ddns.net/");
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "avnPkfmWSnf2Y5aKf1497LahLD3eKuwn");
byte[] result = null;
HttpResponseMessage response = client.GetAsync(fileUrl).Result;
if (response.StatusCode == System.Net.HttpStatusCode.Found)
{
var locaton = response.Headers.Location.AbsoluteUri;
HttpResponseMessage secondResponse = client.GetAsync(locaton).Result;
if (secondResponse.StatusCode == System.Net.HttpStatusCode.Found)
{
string finalUrl = secondResponse.Headers.Location.OriginalString;
HttpResponseMessage thirdResponse = client.GetAsync(finalUrl).Result;
result = thirdResponse.Content.ReadAsByteArrayAsync().Result;
Console.Write(Encoding.UTF8.GetString(result));
}
}
}
}