服务器中的SFTP上载失败

本文关键字:上载 失败 SFTP 服务器 | 更新日期: 2023-09-27 17:59:27

我有一个用C#完成的FTP解决方案来上传文件。适用于本地和QA实例。FTP连接是安全的。要移动的文件大小在2-3 KB之间。将解决方案移动到生产服务器(Windows Server 2012 R2)后,将引发以下错误。

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()

代码-

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

我尝试了以下解决方案。1.防火墙中允许的出站端口21、22、898、9902.将FTPWebRequest超时时间增加到100分钟3.将代理设置为空

这些对我都没有帮助。有什么建议吗?

服务器中的SFTP上载失败

我得到了解决方案。生产服务器位于另一个防火墙后面,该防火墙不在我的控制或访问范围内。这是生产服务器的一层。我与主机提供商合作,他们对协议级别进行了更改,以允许FTP连接。现在,文件开始上传到FTP目录中,没有任何问题。

感谢所有帮助我的人。