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);
}