cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Organizing your research is important to easily review and share it. Learn how Kim uses Dropbox to gather and organize her research right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
1
Ask
2
Comments

"The response ended prematurely" after one hour of a file download

"The response ended prematurely" after one hour of a file download

neilgd
Explorer | Level 4

I'm trying to download a large (80Gb) file from the Dropbox servers.

 

After *exactly* one hour, I get an exception indicating that the Dropbox servers have dropped the connection (System.IO.IOException: The response ended prematurely. in .NET)

 

I have tried using both the .NET HttpClient (for a shared link from dropboxusercontent.com) and the Dropbox API.

 

Any ideas? Thanks!

1 Accepted Solution

Accepted Solutions

Re: "The response ended prematurely" after one hour of a file download

Greg-DB
Dropboxer

If these are failing after an hour, that would be due to the server timeout. There isn't an option to delay or prevent that.

 

Content-download style endpoints, such as /2/files/download, support "range requests" though. I recommend using range requests to continue downloading the rest of the data if a connection fails, and repeat as needed for up to one hour per connection.

 

Here's a basic example showing how to do that:

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://content.dropboxapi.com/2/files/download");
request.Headers.Add("Authorization", "Bearer " + accessToken);
request.Headers.Add("Dropbox-API-Arg", "{\"path\":\"/test.txt\"}");  // in real code, use a JSON library to build this
request.Headers.Add("Range", "bytes=5-");  // this requests the rest of the file after the first 5 bytes

HttpResponseMessage response = await client.SendAsync(request);
Stream stream = response.Content.ReadAsStream();  // read from stream as desired

For instance, in this example, this would be helpful if the app previously only downloaded the first 5 bytes of the file, and needs the rest. In your actual use, you would programmatically set the byte range as needed based on what you successfully downloaded so far of course.

View solution in original post

2 Replies 2

Re: "The response ended prematurely" after one hour of a file download

Greg-DB
Dropboxer

If these are failing after an hour, that would be due to the server timeout. There isn't an option to delay or prevent that.

 

Content-download style endpoints, such as /2/files/download, support "range requests" though. I recommend using range requests to continue downloading the rest of the data if a connection fails, and repeat as needed for up to one hour per connection.

 

Here's a basic example showing how to do that:

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://content.dropboxapi.com/2/files/download");
request.Headers.Add("Authorization", "Bearer " + accessToken);
request.Headers.Add("Dropbox-API-Arg", "{\"path\":\"/test.txt\"}");  // in real code, use a JSON library to build this
request.Headers.Add("Range", "bytes=5-");  // this requests the rest of the file after the first 5 bytes

HttpResponseMessage response = await client.SendAsync(request);
Stream stream = response.Content.ReadAsStream();  // read from stream as desired

For instance, in this example, this would be helpful if the app previously only downloaded the first 5 bytes of the file, and needs the rest. In your actual use, you would programmatically set the byte range as needed based on what you successfully downloaded so far of course.

Re: "The response ended prematurely" after one hour of a file download

neilgd
Explorer | Level 4

Thank you! That works a treat.

Who's talking

Top contributors to this post

  • User avatar
    neilgd Explorer | Level 4
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?