Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need some advice on best way to programmatically download SQL Server Compact Edition .sdf files from a server. I've tried 2 different ways and I like one way better, but due to security issues with the client once the project is deployed, my boss is asking me to go the other direction that ends up corrupting the .sdf files once they are downloaded.

Method 1 (The way I like):

C#
int bytesRead = 0;
byte[] buffer = new byte[2048];

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(FTPLocation + "sdfFile"));

request.Proxy = null;

request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(userNameInput, passwordInput);                                                
request.Method = WebRequestMethods.Ftp.DownloadFile;

Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream("File Location to Store .sdf file", FileMode.Create);

while (true)
{
     bytesRead = reader.Read(buffer, 0, buffer.Length);
     if (bytesRead == 0)
          break;

     fileStream.Write(buffer, 0, bytesRead);
}

fileStream.Close();
fileStream.Dispose();
reader.Close();
reader.Dispose();


Method 2 (This causes the corruption of the files)
C#
WebClient dbClient = new WebClient();
dbClient.DownloadFile(downloadLocation + "sdfFile", "File Location to store .sdf file");


Has anyone gotten the second method to work without any issues? I can't get a clean test using the 2nd method. The security issue is that the client's laptops can't login to FTP server to download files, they can only download from a website.
Posted
Comments
Matt T Heffron 23-Sep-13 16:01pm    
Just a guess but are the Accept header in the request and the server's Content-Type: in the response consistent with transmitting the file unmodified?
joshrduncan2012 23-Sep-13 16:07pm    
Are you referring to what is needed for an HTTPWebRequest?
Matt T Heffron 23-Sep-13 16:14pm    
Yes, basically.
Just make sure the file is transferred equivalently to a Binary mode FTP.
joshrduncan2012 23-Sep-13 16:22pm    
That's essentially what I was describing in Method 1 (which works perfectly). However, Method 2 (shown above) is what my boss wants and I can't convince him otherwise that I can't guarantee that Method 2 works since I can't get a clean test using Method 2.
Matt T Heffron 23-Sep-13 16:28pm    
What are the headers the server is providing in the response?
Verify they are reasonable for a binary file download?
The server must believe the file is a binary file and transmit it as such.
(E.g., if the server is saying "Content-type: text/html" you know you've got a problem!)

1 solution

Try this console app that will show you all of the headers returned by the server.
Url to the SDF file as the command line argument.
C#
using System;
using System.Net;

namespace HttpHeaders
{
  class Program
  {
    static void Main(string[] args)
    {
      if (args.Length < 1)
        return; // just quit. this is a hack tool after all...
      string uri = args[0];
      var request = (HttpWebRequest)WebRequest.Create(uri);
      request.Method = "HEAD";
      var response = request.GetResponse();
      foreach (var key in response.Headers.Keys)
      {
        Console.Write((string)key);
        Console.Write(": ");
        Console.WriteLine(response.Headers[(string)key]);
      }
    }
  }
}
 
Share this answer
 
Comments
joshrduncan2012 24-Sep-13 9:34am    
Matt,

Should I be concerned about the result of what I'm seeing when I run this code? I'm not seeing anything.

if (args.Length < 1)
return;

This line gets executed every time.
Matt T Heffron 24-Sep-13 12:29pm    
This is just checking for the url on the command line.
e.g.:
ThisProgram.exe URL-to-sdf-file-here

If you're executing this directly in Visual Studio 2012 (2010 will be similar):
on the Project menu, select the "ProjectName Properties..." entry.
Select the "Debug" item on the left and enter the url in the "Command line arguments" box.
Matt T Heffron 24-Sep-13 17:29pm    
Thanks for accepting the answer!
What was the solution to the problem?
joshrduncan2012 26-Sep-13 15:59pm    
My boss suggested that I place the .sdf file in a zip file and then try uploading and downloading it and that worked. Btw, it's nice to have a boss who is also a programmer, too. I double checked the server headers with what you presented. No issues there.
joshrduncan2012 1-Oct-13 15:26pm    
Matt,

It turns out the method described in my response below doesn't help. I'm still getting corruption issues when I'm trying to download the sdf files using webclient.downloadfile() method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900