我试图连接到FTP服务器并检索文件,但收到此错误(503错误的命令序列)

时间:2017-04-20 07:37:40

标签: asp.net vb.net

任何想知道如何使这项工作的人?我试图连接并检查服务器是否可用于下载。

    Protected Function Check_FTPServerAccessable(Dir_User As String, Dir_Passw As String) As Boolean
    Check_FTPServerAccessable = False        
    Dim Dir_tpWebRequest As FtpWebRequest
    Dim Dir_tpWebResponse As FtpWebResponse

    Dir_tpWebRequest = WebRequest.Create("ftp://130.100.100.100/outbound/indexfile")

    Dim cc As New CredentialCache()
    cc.Add(New Uri("http://intranet/"), "NTLM", New NetworkCredential(Dir_User, Dir_Passw, "domain"))
    Dir_tpWebRequest.Credentials = cc
    '
    ' Conect to direrctory and retrive outbound index file for available downloads
    '
    Try
        Dir_tpWebResponse = Dir_tpWebRequest.GetResponse()'This code produces 503 Bad sequence of command
        Check_FTPServerAccessable = True

    Catch ex As Exception
        If Request.IsLocal Then
            System.Web.HttpContext.Current.Session("CurrentError") = ex.Message
            System.Web.HttpContext.Current.Session("sv_Ec") = "003"
        Else
            System.Web.HttpContext.Current.Session("CurrentError") = "Error processing page."
        End If

        Response.Redirect("ErrorPage.aspx")

        Exit Function
    End Try
End Function

1 个答案:

答案 0 :(得分:0)

如何尝试和调整此代码:

' Get the object used to communicate with the server.  
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://130.100.100.100/outbound/indexfile"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
' This example assumes the FTP site uses anonymous logon.  
request.Credentials = New NetworkCredential(Dir_User, Dir_Passw)
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Console.WriteLine("Download Complete, status {0}", response.StatusDescription)
reader.Close()
response.Close()