当前位置: 首页 > 知识库问答 >
问题:

从REST API下载一个文件,serveral 302在C#中发现了给出问题,但在Postman和browser中做得很好

邹弘
2023-03-14

我调用了黑板学习API来下载一个文件。网址是

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

当我点击浏览器上的链接时,我看到在浏览器上呈现内容之前有2个更多的找到302。

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

https://blackboard.ddns.net/bbcswebdav/courses/1001/Blackboard密码txt?VxJw3wfC56=1553767343

我通过发送承载令牌在邮递员上进行了尝试,并请求一个GET请求来调用该方法,它也收到了内容。

我必须编写C#代码来接收文件的内容,但我没有接收文件,但接收错误。我一直在努力解决这个问题,但没有成功。你对此有什么想法吗?请帮忙。

我在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;
        }
    }
}

在读了一篇帖子后,我怎样才能获得系统。网Http。HttpClient是否不遵循302重定向?,我试着按照302重定向并更改代码,如下所示,但最后还是出现了同样的错误。

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));
                    }
                }               
            }
        }

共有1个答案

茅慈
2023-03-14

我试过使用不同的HTTP客户端。NET(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");

}
 类似资料: