Request entity too large (413). (Exception from HRESULT: 0x8019019D) in background file downloading

1

I am creating an app, in which I need to download some files from Google Drive, count of files ranges from 1 to 1000+ according to users. I thought to use background download API from WinRT & Google Drive REST API. I implemented like given below. Now the problem is I am getting Request entity too large (413). (Exception from HRESULT: 0x8019019D) at random time. After that each file downloading task either throw that error or "Bad Request (400)". What could be cause of it? My requirement is to download files even if app goes to suspended state.

I also monitored the download request in fiddler, there's no indication of cause of exception.

Exception details:

Request entity too large (413). (Exception from HRESULT: 0x8019019D)

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at XXXXX.YYYY.ZZZZZZ.<QueueDownloadFilesAsync>d__13f.MoveNext() in e:\Projects\xxx\yyy\aaa\qwerty.cs:line 1350
private static List<Task> DownloadCompletionTasks = new List<Task>();

public static async Task DownloadAllFilesAsync(string AccessToken)
{
    foreach (var _file in _NewFiles)
    {
        var TargetFolder = await GetTargetFolderForNewFolderCreationAsync(_file.DepthLevel, _file.Path);
        if (TargetFolder != null)
        {
            DownloadCompletionTasks.Add(QueueDownloadFilesAsync(AccessToken, _file.DownloadUrl, TargetFolder, _file.FileName, CreationCollisionOption.ReplaceExisting));
        }
    }
}

await Task.WhenAll(DownloadCompletionTasks);

public static async Task QueueDownloadFilesAsync(string AccessToken, string fileUrl, StorageFolder folder, string fileName, CreationCollisionOption CollisionOption)
{
    try
    {
        if ((fileName.ToValidFileName() + folder.Path).Length < 256)
        {
            var file = await folder.CreateFileAsync(fileName.ToValidFileName(), CollisionOption);
            backgroundDownloader.SetRequestHeader("Authorization", "bearer" + " " + AccessToken);
            var DownloadOperation = backgroundDownloader.CreateDownload(new Uri(fileUrl), file);

            var progress = new Progress<DownloadOperation>(ProgressCallback);
            await DownloadOperation.StartAsync().AsTask(progress);    //---Exception occures here---
            ResponseInformation response = DownloadOperation.GetResponseInformation();
            if (response.StatusCode == 200)
            {
                Debug.WriteLine("Download successfull.");
            }
            else
            {
                Debug.WriteLine("Error occurred while downloading file. HTTP error code: " + response.StatusCode);
            }
        }
    }

    catch (Exception ex)
    {
        if (ex.ToString().Contains("entity too large"))
        {
            Debug.WriteLine("File name: " + fileName + "\nPath: " + folder.Path + "\n\n" + ex.ToString());
        }
        else
        {
            Debug.WriteLine(ex.ToString());
        }
    }
}
c#
windows-8
windows-runtime
microsoft-metro
winrt-async
asked on Stack Overflow Apr 2, 2014 by Farhan Ghumra • edited Apr 3, 2014 by Farhan Ghumra

1 Answer

1

Your server may be configured to send back only certain bytes of data to prevent DOS attacks. This happens typically with IIS and WCF Services etc.

Another setting constantly talked about with upload issues is uploadReadAheadSize. Because, you're trying to download the file and not upload, we can skip that.

If it is just at IIS level for you:

<system.webServer> 
 <security> 
    <requestFiltering> 
       <requestLimits maxAllowedContentLength="5242880" /> 
    </requestFiltering> 
 </security> 
</system.webServer>

If it is WCF,

Settings like maxReceivedMessageSize etc. are modified to accommodate for the increased size.

the below setting sets the max size to 500 MB.

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="5242880">
      </binding>
    </basicHttpBinding>
  </bindings>  
</system.serviceModel>

413 errors used to be previously 400 errors, till things got changed in IIS or WCF.

answered on Stack Overflow Apr 2, 2014 by Raja Nadar • edited Apr 2, 2014 by Raja Nadar

User contributions licensed under CC BY-SA 3.0