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

.net FtpWebRequest/FtpWebResponse:即使在成功连接后提供了错误的密码,连接也成功,.net,vb.net,.net-4.0,ftpwebrequest,.net,Vb.net,.net 4.0,Ftpwebrequest,我正在使用用户提供的用户/密码测试FTP连接 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}", _

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

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
如果我独立运行单元测试,一切都会正常工作。但是如果我尝试一个有序的测试,在
FTP\u测试不成功之前运行
FTP\u测试成功
,则
FTP\u测试不成功
test失败:即使在成功连接之后提供了错误的密码,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
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
我如何“重置”FTP连接,以便
FTP\u测试\u不成功
实际正确测试连接

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
下面是我的单元测试:

'''<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
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
由于上的回答,我刚刚将
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