Code I am using:

using (WebClient client = new WebClient()) { 
    client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
    client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
    client.Headers.Add(HttpRequestHeader.ContentEncoding, "UTF-8");
    client.Headers.Add("Authorization", "Bearer" + accessToken); 
    client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
    client.Credentials = credential;
    Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + folderServerRelativeUrl + "/" + fileName + "')/$value");
    //string result = client.DownloadString(endpointUri);
    byte[] data = client.DownloadData(endpointUri);
    FileStream outputStream = new FileStream(path + fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None);
    outputStream.Write(data, 0, data.Length); outputStream.Flush(true);

I am getting exception at byte[] data = client.DownloadData(endpointUri);. The endpoint URI I have generated, its working fine when I am hitting it in browser or using POSTMAN.

I am able to download the file using endpoint URI, its just not working through code.

I am lost and couldn't find anything.

Any help will be useful for me.

  • Answer to this is - you don't need any headers, just need to pass auth cookies and it will work correctly. – user11099777 Jul 12 '19 at 7:18
  • you can answer your own question below. please include complete code you used so that it will benefit other community users and this question will not be shown in Unanswered questions. plus you will get some medals:P – Ganesh Sanap Jul 18 '19 at 7:03

I have refactored the code and using HttpClient to download the file.

private static async Task DownloadDocumentAsync(string webUrl, string folderServerRelativeUrl, string fileName, string path)
                string accessToken = "Access Token";
                //Creating Endpoint Uri
                Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + folderServerRelativeUrl + "/" + fileName + "')/$value");
                using (var httpClient = new HttpClient())
                    httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");
                    httpClient.DefaultRequestHeaders.Add("binaryStringRequestBody", "true");
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    httpClient.DefaultRequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

                    using (var request = new HttpRequestMessage(HttpMethod.Get, endpointUri))

                        byte[] contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsByteArrayAsync();
                        FileStream stream = new FileStream(path + fileName, FileMode.Create, FileAccess.Write, FileShare.None);
                            //await contentStream.CopyToAsync(stream);
                            stream.Write(contentStream, 0, contentStream.Length);
            catch(Exception ex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.