0

我已调用 Blackboard Learn API 来下载文件。网址是

https://blackboard.ddns.net/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download

当我单击浏览器上的链接时,我看到在浏览器上呈现内容之前还有 2 个302 Found 。

https://blackboard.ddns.net/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download 状态码:302

https://blackboard.ddns.net/bbcswebdav/xid-1407_1?VxJw3wfC56=1553768183&Kq3cZcYS15=6716bb2d5bc740c29eb11e01d75bc913&3cCnGYSz89=LhqjIQu9ReJ6IaoXzfPALpMer72momaz%2BYWP%2 _

https://blackboard.ddns.net/bbcswebdav/courses/1001/Blackboard%20code.txt?VxJw3wfC56=1553767343&Kq3cZcYS15=6716bb2d5bc740c29eb11e01d75bc913&3cCnGYSz89=uSziSDgibsV8PoWygH4spxpX7lOdDGwAvtlIHvrbPxM%3D Status code: 200

我通过发送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));
                    }
                }               
            }
        }
4

1 回答 1

0

我尝试过使用 .NET 中可用的不同 HTTP 客户端(HttpWebRequest/Response、WebClient、HttpClient),但总是收到 500 内部服务器错误。因此,我决定使用第三方 HTTP 客户端进行测试,幸运的是 RestSharp 为我工作。

public ActionResult Index()
{
var client = new RestClient("https://blackboard.ddns.net/");

var request = new RestRequest("/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download", Method.GET);           

client.AddDefaultHeader("Authorization", "Bearer ZVAFzAuZ1ujYRENLrjNv3b8UWWvgRic4");

var response = client.Execute(request);           

return File(response.RawBytes, "text/plain", "MyTest.txt");

}
于 2019-04-01T11:50:22.193 回答