title: FTP File Download C #
body: <p>In my code I want to download a specific File from FTP-Server to my local machine.I am getting an exception like below</p>
  
  <p><CODE>System.InvalidCastException: Unable to cast object of type: 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest' at FTPapp.ftpdownload.Download(String remoteFile, String localFile)</CODE></p>
  
  <p>My code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace FTPapp
              {
                  public partial class ftpdownload : Form
                  {
                      private string Host = null;
                      private string user = null;
                      private string pass = null;
                      private string SSH = null;
                      private FtpWebRequest ftprequest = null;
                      private FtpWebResponse ftpresponse = null;
                      private Stream ftpStream = null;
                      private int buffersize = 2048;
                      public ftpdownload()
                      {
                          InitializeComponent();
                      }
  
                      public ftpdownload(string hostIP, string username, string password ,string SSHkey)
                      {
                          Host = hostIP;
                          user = username;
                          pass = password;
                          SSH = SSHkey;
  
                      }
                      public void Download(string remoteFile, string localFile)
                      {
                          try
                          {
  
                              ftprequest = (FtpWebRequest)FtpWebRequest.Create(Host + "/" + remoteFile);
                              ftprequest.Credentials = new NetworkCredential(user, pass ,SSH);
                              ftprequest.UseBinary = true;
                              ftprequest.UsePassive = true;
                              ftprequest.KeepAlive = true;
  
                              ftprequest.Method = WebRequestMethods.Ftp.DownloadFile;
                              ftpresponse = (FtpWebResponse)ftprequest.GetResponse();
                              ftpStream = ftpresponse.GetResponseStream();
                              FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                              byte[] byteBuffer = new byte[buffersize];
                           `enter code here`   int bytesRead = ftpStream.Read(byteBuffer, 0, buffersize);
  
                              try
                              {
                                  while (bytesRead &gt; 0)
                                  {
                                      localFileStream.Write(byteBuffer, 0, bytesRead);
                                      bytesRead = ftpStream.Read(byteBuffer, 0, buffersize);
                                  }
                              }
                              catch (Exception ex)
                              {
                                  Console.WriteLine(ex.ToString());
  
                              }
                              localFileStream.Close();
                              ftpStream.Close();
                              ftpresponse.Close();
                              ftprequest= null;
                          }
                          catch (Exception ex)
                          {
                              Console.WriteLine(ex.ToString());
                              MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
  
                          }
                      }
  
                      private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
                      {
  
                      }
  
                      private void Form1_Load(object sender, EventArgs e)
                      {
                          txtServer.Text = "http://ftpsite.com";
                          txtUsername.Text = "";
                          txtPassword.Text = @"";
                          txtFilename.Text = @"";
                          txtSSHKey.Text = "";
                      }
  
                      private void BtnDownload_Click(object sender, EventArgs e)
                      {
                          ftpdownload ftp = new ftpdownload("http://ftpsite.com" ,"","","");
                          ftp.Download(@"Remotepath", @"local path");
                      }
  
  
                  }
              }
  </code></pre>