0

I have called a Blackboard Learn APIs to download a file. The Url is

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

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

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

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));
                    }
                }               
            }
        }
Simant
  • 3,142
  • 4
  • 32
  • 61
  • The best way to fix problem is to use a sniffer like wireshark or fiddler. Compare working sniffer data with non working fiddler data. Look at the working request headers and make non working headers the same. There are too many reasons for failure to start guessing at the correct answer. Only way for sure is to use sniffer. – jdweng Mar 28 '19 at 10:23

1 Answers1

0

I have tried with different HTTP clients available in .NET (HttpWebRequest/Response, WebClient, HttpClient), but always received 500 Internal Server Error. So, I decided to test with a third-party HTTP client and luckily RestSharp worked for me.

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

}
Simant
  • 3,142
  • 4
  • 32
  • 61