从具有多个 302 的 REST API 下载文件发现在 C# 中出现问题但在 Postman 和浏览器中运行良好

Downloading a file from a REST APIs which has serveral 302 Found giving issue in C# but done well in Postman and browser

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

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

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

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

https://blackboard.ddns.net/bbcswebdav/xid-1407_1?VxJw3wfC56=1553768183&Kq3cZcYS15=6716bb2d5bc740c29eb11e01d75bc913&3cCnGYSz89=LhqjIQu9ReJ6IaoXzfPALpMer7momaz%2BwYxyWPG3YFY%3D Status code: 302

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

我在 Postman 上尝试发送 Bearer token 并请求 GET 请求来调用该方法,它也收到了内容。

我必须编写 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;
        }
    }
}

阅读 post 后,我尝试遵循 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));
                    }
                }               
            }
        }

我尝试使用 .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");

}