Downloading files from a server

I have put the files for an infield update on my web server and when I try to get the files to download with the code below, I get the HTML 404 error page instead. The files are there on the server as I can see them with the FTP app I use to upload them there.

I also have an app.xml file that contains the version numbers to check if they need to be downloaded and this one downloads without any issues.

It would appear to be an extension naming issue and the web server doesn’t know how to deal with this. I could of course just rename them as XML on the server and re-write them as .HEX when I download them.

Any other options?


static bool GetFileFromServer(string url, string filename)
{
    HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
    //
    // Set request.KeepAlive to use a persistent connection.
    //
    request.KeepAlive = true;
    //
    // Get a response from the server.
    //
    WebResponse resp = null;

    try
    {
        resp = request.GetResponse();
    }
    catch (Exception e)
    {
    }

    // Get the network response stream to read the page data.
    if (resp != null)
    {
        FileStream file = null;
        Stream respStream = resp.GetResponseStream();
        byte[] byteData = new byte[2048];
        char[] charData = new char[2048];
        int bytesRead = 0;
        Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
        //
        // Create the file on the FLASH disk to hold the file we will download
        //

        try
        {
            file = new FileStream(filename, FileMode.Create);
        }
        catch (Exception ex)
        {
            return (false);
        }
        //
        // allow 20 seconds for reading the stream
        //
        respStream.ReadTimeout = 20000;
        //
        // If we know the content length, read exactly that amount of
        // data; otherwise, read until there is nothing left to read.
        //
        if (resp.ContentLength != -1)
        {
            for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
            {
                Thread.Sleep(1);

                try
                {
                    bytesRead = respStream.Read(byteData, 0, byteData.Length);
                }
                catch (Exception e)
                {
                }
                if (bytesRead == 0)
                {
                    break;
                }
                dataRem -= bytesRead;
                //
                // Save the bytes to the file
                //
                file.Write(byteData, 0, bytesRead);
            }
        }
        else
        {
            //
            // Read until the end of the data is reached.
            //
            while (true)
            {
                //
                // If the Read method times out, it throws an exception,
                // which is expected for Keep-Alive streams because the
                // connection isn't terminated.
                //
                try
                {
                    Thread.Sleep(1);

                    bytesRead = respStream.Read(byteData, 0, byteData.Length);
                }
                catch (Exception)
                {
                    bytesRead = 0;
                }
                //
                // Zero bytes indicates the connection has been closed
                // by the server.
                //
                if (bytesRead == 0)
                {
                    break;
                }
                //
                // Save the bytes to the file
                //
                file.Write(byteData, 0, bytesRead);
            }
        }
        //
        // Close the file on FLASH
        //
        file.Close();
        //
        // Close the response stream. For Keep-Alive streams, the
        // stream will remain open and will be pushed into the unused
        // stream list.
        //
        resp.Close();
    }
    return (true);
}

Have you sniffed the communication with netmon/wireshark? Have you emulated the request from a browser and got the file, or the 404?

Yes. I tried that just after the post here and it came back with 404 error.

Changing to XML extension worked.

1 Like

@ Dave McLaughlin - Is your web server running IIS?

It is a Windows hosted system on GoDaddy.

[EDIT] It’s running ASP.NET 4.0/4.5

He said that he’s getting 404, so looks like the web server is there.

Can you navigate to the file using web browser from the computer? If files are on the server, its not automatically means that they are accessible through the web server.

Also are you 100% sure that you have correct url in your program?

@ Dave McLaughlin - That looks like an IIS server. IIS by default doesn’t serve every file type. In IIS Manager, under your specific web site node, open the MIME Types section and add .hex. Add a MIME Type (IIS 7) | Microsoft Learn and https://support.microsoft.com/en-us/kb/326965 have more information.

2 Likes

IIS does something called MIME type filtering. If it dosn’t know a specific file extension’s MIME type, it returns a 404 error.

1 Like

Yes. If I change the extension to XML it downloads the file. I think it is the MIME type as per John’s post. I’ll fix this tomorrow.

We bumped into that same issue when storing hex files on an IIS server.
You need to add the HEX extension in the accepted mime types and have the content type set to application/octet-stream.