【问题标题】:VB.NET Testing ftp connectionVB.NET 测试 ftp 连接
【发布时间】:2014-07-06 03:21:57
【问题描述】:

所以我正在尝试将文件上传到我的 ftp,但我希望它测试 FTP 详细信息是否正确。我在下面找到了这段代码,虽然它在连接时显示“存在”但当它无法连接时 [with fake details] 它什么也没做(我想要一个错误来显示它无法连接)

Imports System.Net

    Dim request = _
    DirectCast(WebRequest.Create _
    ("ftp://ftp.example.com/folder_here/"), FtpWebRequest)

    request.Credentials = _
    New NetworkCredential("user_here", "pass_here")

    request.Method = WebRequestMethods.Ftp.ListDirectory

    Try
        Using response As FtpWebResponse = _
        DirectCast(request.GetResponse(), FtpWebResponse)
            ' Folder exists here
            MsgBox("exists!")
        End Using

    Catch ex As WebException
        Dim response As FtpWebResponse = _
        DirectCast(ex.Response, FtpWebResponse)
        'Does not exist
        If response.StatusCode = _
        FtpStatusCode.ActionNotTakenFileUnavailable Then
            MsgBox("Doesn't exist!")
        End If
    End Try

我该如何解决这个问题?

【问题讨论】:

    标签: vb.net ftp


    【解决方案1】:

    我认为 using 块可能正在捕获异常。试试这个

    Try
        Dim response As FtpWebResponse = _
        DirectCast(request.GetResponse(), FtpWebResponse)
            ' Folder exists here
        MsgBox("exists!")
    
    Catch ex As WebException
        Dim response As FtpWebResponse = _
        DirectCast(ex.Response, FtpWebResponse)
        'Does not exist
        If response.StatusCode = _
        FtpStatusCode.ActionNotTakenFileUnavailable Then
            MsgBox("Doesn't exist!")
        End If
    End Try
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 2018-06-18
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      相关资源
      最近更新 更多