服务器中的SFTP上载失败

时间:2015-06-23 16:50:05

标签: c# ftp

我在C#中完成了一个FTP解决方案来上传文件。在本地和QA实例中工作得很好。 FTP连接是安全的。要移动的文件大小在2 - 3 KB范围内。将解决方案移至Production Server(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,990用于防火墙中的出站 2.将FTPWebRequest超时增加到100分钟 3.将代理设置为NULL

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

1 个答案:

答案 0 :(得分:3)

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

感谢所有帮助过我的人。