我已调用 Blackboard Learn API 来下载文件。网址是
当我单击浏览器上的链接时,我看到在浏览器上呈现内容之前还有 2 个302 Found 。
我通过发送Bearer 令牌并请求 GET 请求来调用该方法来尝试使用Postman ,它也收到了内容。
我必须编写 C# 代码来接收文件的内容,但我没有收到文件但收到 500 InternalServerError。我一直在尝试解决这个问题,但没有运气。你对此有什么想法吗?请帮忙。
我在 C# 控制台应用程序中的代码如下:
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;
}
}
}
阅读帖子后如何让 System.Net.Http.HttpClient 不遵循 302 重定向?,我尝试遵循 302 重定向并更改代码如下,但最后得到相同的 500 InternalServerError。
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));
}
}
}
}