Sreeju S Kumar
Sreeju S Kumar

Reputation: 61

SFTP Upload fails in Server

I have an FTP solution done in C# to upload files. Works perfect in local and QA instances. The FTP connection is secured. The size of file to move is in the range of 2 - 3 KB. Once the solution is moved to Production Server (Windows Server 2012 R2), the below error is thrown.

System.Net.WebException: System error. ---> System.Net.InternalException: System error.
   at System.Net.PooledStream.PrePush(Object expectedOwner)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.AttemptedRecovery(Exception e)
   at System.Net.FtpWebRequest.SubmitRequest(Boolean async)
   --- End of inner exception stack trace ---
   at System.Net.FtpWebRequest.GetRequestStream()

The Code -

try
            {
                Stream ftpStream = null;
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory + "/" + fileName);
                Logger.Information(string.Format("THE URI TO UPLOAD THE FILE IS - {0}", ftpRequest.RequestUri));
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate CERTIFICATE, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.EnableSsl = enableSSL;
                ftpRequest.Proxy = null;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                /* Establish Return Communication with the FTP Server */
                ftpStream = ftpRequest.GetRequestStream();
                /* Open a File Stream to Read the File for Upload */
               // FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];

                int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */

                    while (bytesSent != 0)
                    {
                        ftpStream.Write(byteBuffer, 0, bytesSent);
                        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                    }

                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                uploadSuccess = false;
                Logger.Error(string.Format("ERROE WHILE UPLOADING FILE {0} TO FTP", fileName), ex);
                Logger.Error(string.Format("EXCEPTION - {0}", ex.ToString()));
            }

I tried the below solutions. 1. Allowed ports 21, 22, 898, 990 for Outbound in Firewall 2. Increased the FTPWebRequest timeout to 100 minutes 3. Set the Proxy as NULL

None of these helped me. Any suggestions?

Upvotes: 3

Views: 1225

Answers (1)

Sreeju S Kumar
Sreeju S Kumar

Reputation: 61

I have got the solution. The Production Servers were behind another Firewall, which was not under my control or access. It was a layer up to the Production Servers. I worked with the hosting provider, and they made protocol level changes to allow FTP connections. Now the files started to get uploaded in the FTP directory without any issues.

Thanks for all who helped me.

Upvotes: 3

Related Questions