我创建了一个控制台应用程序,它从FTP服务器读取CSV文件,并在验证后将记录转储到数据库。
当我从本地机器上尝试代码时,它工作得很好。没有错误,没有错误。但是当我把它放在 Azure 虚拟机上来安排它时。或者当我创建 Azure WebJob 以创建计划作业时,我在访问文件时遇到以下错误。
远程服务器返回错误:(500) 语法错误,命令无法识别。在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj) 在 System.Net.FtpWebRequest.RequestCallback(Object obj) 在 System.Net.CommandStream.Dispose(Boolean disposing) 在 System.IO.Stream.Close() 在 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.FinishRequestStage(RequestStage 阶段) 在 System.Net.FtpWebRequest.GetResponse() 在 Bethesda_DataUpload.Program.Main(String[] args) 在 D:\Projects\Program.cs:line 35
我正在使用以下代码连接到 FTP 服务器
string dataFile = "ftp://testftp-L.cloudapp.net/Inbound/sampleFile.csv";
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(dataFile));
request.UsePassive = false;
request.UseBinary = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(ftpUsrName, ftpPassword);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Proxy = WebRequest.DefaultWebProxy;
request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
List<string> entries = new List<string>();
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
我已经尝试过KeepAlive和UsePassive的所有组合,例如
组合1
request.UsePassive = false;
request.KeepAlive = false;
组合2
request.UsePassive = false;
request.KeepAlive = true;
组合3
request.UsePassive = true;
request.KeepAlive = false;
组合4
request.UsePassive = true;
request.KeepAlive = true;
我应该使用什么代码?