title: FTP request to server can not connect remote server
body: <p>I found a number of similar articles here but did not manage to solve my problem still.
  I am trying to upload a text file to an ftp server. I used a number of methods and all of them i get same error : "Can not connect to remote server"</p>
  
  <p>Method1 :
  filename is the full path where the file is located</p>
  
  <pre><code>    private string Upload(string ftpServer, string userName, string password, string filename)
      {
          string reply = "Success";
          try
          {
              using (System.Net.WebClient client = new System.Net.WebClient()) //System.Net.WebClient client = new System.Net.WebClient()
              {
                  client.Credentials = new System.Net.NetworkCredential(userName, password);
                  client.Proxy = new WebProxy();
                  FileInfo fi = new FileInfo(filename);
                  client.UploadFile(ftpServer + "//" + fi.Name, "STOR", filename);
              }
          }
          catch (Exception ex)
          {
              reply = ex.Message;
          }
          return reply;
  }
  </code></pre>
  
  <p>Method2: </p>
  
  <p>filename = "D:\folder\file.txt"
      public static void uploadFileUsingFTP(string filename)
              {
                 FileInfo fileInf = new FileInfo(filename);
                 string uri = "ftp://" + serverIP + "/" + fileInf.Name;</p>
  
  <pre><code>            FtpWebRequest reqFTP; 
              // Create FtpWebRequest object from the Uri provided
              reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
              reqFTP.Proxy = null;
              // Provide the WebPermission Credintials
              reqFTP.Credentials = new NetworkCredential(user, pass); 
  
              // By default KeepAlive is true, where the control connection is not closed
              // after a command is executed.
              reqFTP.KeepAlive = false; 
  
              // Specify the command to be executed.
              reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
  
              // Specify the data transfer type.
              reqFTP.UseBinary = true; 
  
              // Notify the server about the size of the uploaded file
              FileStream fs = File.OpenRead(filename); 
              reqFTP.ContentLength = fileInf.Length; 
              // The buffer size is set to 2kb
              int buffLength = Convert.ToInt32(fs.Length);
              byte[] buff = new byte[buffLength];
              int contentLen; 
  
              try
              {
                  // Stream to which the file to be upload is written
                  Stream strm = reqFTP.GetRequestStream(); 
  
                  // Read from the file stream 2kb at a time
                  contentLen = fs.Read(buff, 0, buffLength); 
  
                  // Till Stream content ends
                  while (contentLen != 0)
                  {
                      // Write Content from the file stream to the FTP Upload Stream
                      strm.Write(buff, 0, contentLen);
                      contentLen = fs.Read(buff, 0, buffLength);
                  } 
  
                  // Close the file stream and the Request Stream
                  strm.Close();
                  fs.Close();
              }
  
              catch (Exception ex)
              {
                  string s = ex.Message;
              }
  
          }
  </code></pre>
  
  <p>Method3:</p>
  
  <pre><code> public static void Sample(string filename)
          {
  
              // Get the object used to communicate with the server.
              FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serverip/"); //test.htm
              request.Method = WebRequestMethods.Ftp.UploadFile;
  
              // This example assumes the FTP site uses anonymous logon.
              request.Credentials = new NetworkCredential (user,passs);
  
              try
              {
                  // Copy the contents of the file to the request stream.
                  StreamReader sourceStream = new StreamReader(filename);
                  byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                  sourceStream.Close();
                  request.ContentLength = fileContents.Length;
  
                  request.Proxy = null;
  
                  Stream requestStream = request.GetRequestStream();
                  requestStream.Write(fileContents, 0, fileContents.Length);
                  requestStream.Close();
  
                  FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  
                  Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
  
                  response.Close();
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              Console.ReadLine();
  
          }
  </code></pre>
  
  <p>Using each of them results in the same problem and yes i am able to connect to the ftp by using filezilla and also transfer files.</p>
  
  <p>I know that i must be missing something very stupid but it is taking me so much time.
  Any suggestion will be appreaciated.</p>