Learn how to make the most out of the Dropbox Community here 💙. 

Forum Discussion

giahh's avatar
giahh
New member | Level 1
3 months ago

Problem with HttpClient

var uri = new Uri("https://www.dropbox.com/scl/fi/l2ltdldscgwr9ho1izx57/TestUpdate-pass.zip?rlkey=cn5adnh45sc3793j60gunn3x8&st=u8atpaop&dl=1");

var httpClientHandler = new HttpClientHandler
{
    AllowAutoRedirect = true
};

var httpClient = new HttpClient(httpClientHandler);
httpClient.Timeout = TimeSpan.FromSeconds(60);

// Accept binary data
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octet-stream"));

HttpResponseMessage response = httpClient.GetAsync(uri).Result;
if (response.IsSuccessStatusCode)
{
    byte[] zipFileData = response.Content.ReadAsByteArrayAsync().Result;
    // Save the zip file to disk
    File.WriteAllBytes("output.zip", zipFileData);
}
else
{
    Console.WriteLine("{0}: {1} ({2})", uri, (int)response.StatusCode, response.ReasonPhrase);
}

I always got the errors: bad zip file, zip file corrupted when downloading the link with the HttpClient or any RestClient similar (Flurl, RestSharp, WebClient, etc.). The code is below. Please help....

Thank you.

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Engineer rankDropbox Engineer

    Hi giahh,

    I'll be happy to help with any issues you're having with Dropbox, but I'll need some more information:

    • Could you confirm if you are seeing errors when the file is downloaded by accessing the link directly?
    • Could you clarify how the file was uploaded to Dropbox? E.g., Dropbox API, desktop client, mobile client, or web.
    • Is the error only with this specific file? Or is it happening with any files you are attempting to download from Dropbox?

    I was able to download the file in question, but was unable to open it due to it being password-protected.

    • giahh's avatar
      giahh
      New member | Level 1

      Hello, the compressed file is ok. The password protected was correct. But the problem with downloaded (file corruption) only happened when downloading using the httpclient on the C# code mentioned. Have you tried it?

    • giahh's avatar
      giahh
      New member | Level 1

      I used this code:

      namespace Test_Dropbox
      {
          internal class Program
          {
              static void Main(string[] args)
              {
                  var uri = new Uri("https://www.dropbox.com/scl/fi/4isq7l500diw6kjjd7hkq/ZipExtractor.zip?rlkey=fsoxq9o89zdqb8mr4ug8ml1uo&st=g7gpau7c&dl=1");
      
                  var httpClientHandler = new HttpClientHandler
                  {
                      AllowAutoRedirect = true,
                  };
      
                  var httpClient = new HttpClient(httpClientHandler);
                  httpClient.Timeout = TimeSpan.FromSeconds(60);
      
                  // Accept binary data
                  httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octet-stream"));
      
                  HttpResponseMessage response = httpClient.GetAsync(uri).Result;
                  if (response.IsSuccessStatusCode)
                  {
                      byte[] zipFileData = response.Content.ReadAsByteArrayAsync().Result;
                      // Save the zip file to disk
                      File.WriteAllBytes("output.zip", zipFileData);
                  }
                  else
                  {
                      Console.WriteLine("{0}: {1} ({2})", uri, (int)response.StatusCode, response.ReasonPhrase);
                  }
      
                  Console.ReadKey();
                  Environment.Exit(0);
              }
          }
      }

      it use HttpClient to Download a zip file without password from the link above and always got this error: 

      The zipped file seem corrupted. How could I fix the code to work properly with dropbox?

      • DB-Des's avatar
        DB-Des
        Icon for Dropbox Engineer rankDropbox Engineer

        Hi giahh 

        It seems that the issue may lie within your integration since the file is downloading as expected from our servers, and the behavior Dropbox is functioning as designed. We recommend further troubleshooting on your end to identify any potential factors affecting the behavior.

        If you have any specific questions about our API functionality, feel free to let us know, and we’ll be happy to assist where possible.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers810 PostsLatest Activity: 5 hours ago
228 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!