FtpWebRequest / FtpWebResponse:即使连接成功后提供了错误的密码,连接也会成功

时间:2013-05-21 14:16:33

标签: .net vb.net .net-4.0 ftpwebrequest

我正在使用用户提供的用户/密码测试FTP连接。

如果我独立运行单元测试,一切正常。但是,如果我尝试进行有序测试,在FTP_Test_Sucessful之前运行FTP_Test_Unsuccessful,则FTP_Test_Unsuccessful测试失败:即使我在连接成功后提供了错误密码,FTP连接也会成功。

Passed          Unit Test   FTP_Test_Unsuccessful       00:00:02.3566157
Passed          Unit Test   FTP_Test_Sucessful          00:00:00.3048244
Failed          Unit Test   FTP_Test_Unsuccessful       00:00:00.2696941
Not Executed    Unit Test   FTP_Test_Sucessful          00:00:00

我如何“重置”FTP连接,以便FTP_Test_Unsuccessful实际正确测试连接。

这是我的单元测试:

'''<summary>
'''basic Unsuccessful FTP test
'''</summary>
<TestMethod()>
Public Sub FTP_Test_Unsuccessful()
    _TestSettings.FTPEnabled = True
    _TestSettings.ChangeFTPCredentials("User", "BadPasword")
    Try
        Assert.IsFalse(_TestSettings.TestFtpSettings())

    Catch ex As AssertFailedException
        Throw 

    Catch ex As Net.WebException
        Dim FTPResponse As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
        Assert.AreEqual(FtpStatusCode.NotLoggedIn, FTPResponse.StatusCode)

    Catch ex As Exception
        Assert.Fail()
    End Try
End Sub

'''<summary>
'''basic Sucessful FTP test
'''</summary>
<TestMethod()>
Public Sub FTP_Test_Sucessful()
    _TestSettings.FTPEnabled = True
    _TestSettings.ChangeFTPCredentials("User", "GoodPasword")
    Assert.IsTrue(_TestSettings.TestFtpSettings())
End Sub

以下是相关代码:

Public Function TestFtpSettings() As Boolean
    Dim FTPConnectionTestedSuccessfully As Boolean = False
    Dim response As FtpWebResponse = GetTestFtpResponse()
    Dim StatusCode As System.Net.FtpStatusCode = response.StatusCode
    response.Close()
    FTPConnectionTestedSuccessfully = (StatusCode = Net.FtpStatusCode.OpeningData)

    Return FTPConnectionTestedSuccessfully
End Function

Private Function GetTestFtpResponse() As FtpWebResponse
    Dim request As FtpWebRequest = GetFTPConnection(WebRequestMethods.Ftp.ListDirectory)
    Dim response As FtpWebResponse = GetFTPResponse(request)

    Return (response)
End Function

Private Function GetFTPConnection(Method As String) As FtpWebRequest
    'Get the object used to communicate with the server.

    Dim FTPAdress As Uri = New Uri(String.Format("ftp://{0}:{1}", _
                                                 Me.FTPServerName, _
                                                 Me.FTPPort))

    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(FTPAdress),  _
                                              FtpWebRequest)

    request.Method = Method
    request.Credentials = New NetworkCredential(Me.FTPUserName,
                                                Me.GetDecryptedPassword)
    Return request
End Function

Public Function GetFTPResponse(request As FtpWebRequest) As FtpWebResponse
    Return DirectCast(request.GetResponse(), FtpWebResponse)
End Function

1 个答案:

答案 0 :(得分:1)

感谢How to Reuse FtpWebRequest Connection上的这个答案,我刚刚将request.KeepAlive = False添加到了我的GetFTPConnection函数中。和所有测试通过。

Private Function GetFTPConnection(Method As String) As FtpWebRequest
    'Get the object used to communicate with the server.

    Dim FTPAdress As Uri = New Uri(String.Format("ftp://{0}:{1}", _
                                                 Me.FTPServerName, _
                                                 Me.FTPPort))

    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(FTPAdress),  _
                                              FtpWebRequest)

    request.Method = Method
    request.KeepAlive = False
    request.Credentials = New NetworkCredential(Me.FTPUserName,
                                                Me.GetDecryptedPassword)
    Return request
End Function