1

I have a valid download url for a file located in google firebase storage and I'm trying to download the file into my application written which is in c# via a HTTP Get request. However, the request fails with the error "WebException: The remote server returned an error: (400) Bad Request." I would really appreciate it if you could point me towards what I'm doing wrong. Thank you in advance for your help! -- Here is reference to the google firebase documentation https://firebase.google.com/docs/storage/web/download-files

Below is the code I am using to download the file:

private void downloadFrame()
{
    //Extract 

    try
    {
        //construct HTTP get request
        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(**link address**);
        httpRequest.Method = "GET";
        httpRequest.ContentType = "text/xml; encoding='utf-8'";

        //send the http request and get the http response from webserver
        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

        Stream httpResponseStream = httpResponse.GetResponseStream();

        // Define buffer and buffer size
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = 0;

        // Read from response and write to file
        FileStream fileStream = File.Create("frame.pcm");
        while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0)
        {
            fileStream.Write(buffer, 0, bytesRead);
        } // end while
    }
    catch (WebException we)
    {
        Debug.Log(we.Response.ToString());
    }

}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56

2 Answers2

0

I realize this is a very old question, but I hit it when I had the same issue and it took me quite a while to find an answer.

The download link I had from Firebase was for a PDF file and contained an escaped "/" character as "%2F". This was being changed back into a "/" on the fly, causing the error.

The answer I required was to alter this behaviour by adding an entry to the web.config file as described here: https://stackoverflow.com/a/12170132

I could then use a simple WebClient to download the file like this:

string targetFileName = @"C:\Temp\Target.pdf"
using (WebClient client = new WebClient())
   {
      Uri downloadURI = new Uri(<Firebase download URL>);
      client.DownloadFile(downloadURI, targetFileName);
   }
Gavin
  • 31
  • 1
  • 6
-1

Note: By default, Cloud Storage buckets require Firebase Authentication to download files. You can change your Firebase Security Rules for Cloud Storage to allow unauthenticated access https://firebase.google.com/docs/storage/web/download-files

Change this and try again.

Gaterde
  • 779
  • 1
  • 7
  • 25