从Azure VM或Azure WebJob中读取FTP文件

时间:2016-09-06 05:59:27

标签: azure ftp readfile ftpwebrequest ftpwebresponse

我创建了一个控制台应用程序,它从 FTP 服务器读取 CSV 文件,并在验证后将记录转储到数据库。

当我尝试使用本地计算机上的代码时,它的工作正常。没有错误,没有错误。但是当我把它放在Azure虚拟机上来安排它时。 或者当我创建Azure WebJob以创建计划作业时,我在访问文件时遇到以下错误。

  

远程服务器返回错误:(500)语法错误,命令   无法识别。在System.Net.FtpWebRequest.SyncRequestCallback(Object   obj)在System.Net.FtpWebRequest.RequestCallback(Object obj)at   System.Net.CommandStream.Dispose(布尔处理)at   System.IO.Stream.Dispose()at的System.IO.Stream.Close()   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阶段)at   System.Net.FtpWebRequest.GetResponse()at   Bethesda_DataUpload.Program.Main(String [] args)in   D:\ Projects \ Program.cs:第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;

我应该使用哪些代码?

0 个答案:

没有答案