服务器中的 SFTP 上传失败
SFTP Upload fails in Server
我有一个用 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、990
2. 将 FTPWebRequest 超时增加到 100 分钟
3. 将代理设置为 NULL
None 其中对我有帮助。有什么建议吗?
我找到了解决办法。生产服务器在另一个防火墙后面,不受我的控制或访问。它是生产服务器的一层。我与托管服务提供商合作,他们对协议级别进行了更改以允许 FTP 连接。现在文件开始上传到 FTP 目录,没有任何问题。
感谢所有帮助过我的人。
我有一个用 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、990 2. 将 FTPWebRequest 超时增加到 100 分钟 3. 将代理设置为 NULL
None 其中对我有帮助。有什么建议吗?
我找到了解决办法。生产服务器在另一个防火墙后面,不受我的控制或访问。它是生产服务器的一层。我与托管服务提供商合作,他们对协议级别进行了更改以允许 FTP 连接。现在文件开始上传到 FTP 目录,没有任何问题。
感谢所有帮助过我的人。