Visual Basic:FTP下载

时间:2011-03-15 21:22:11

标签: vb.net ftp

我尝试了不同的代码,其中没有代码用于从示例DriveHQ下载文件。

但我确实设法上传,所以Host似乎很好。

编辑*

sub()
  GetFile("downloadme.txt", "DESKTOP")
    End Sub

    Public Function GetFile(ByVal Name As String, ByVal DestFile As String) As Boolean
        Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://ftp.drivehq.com/" & "" & Name), FtpWebRequest)
        oFTP.Credentials = New NetworkCredential("user", "passw")
        oFTP.Method = WebRequestMethods.Ftp.DownloadFile
        oFTP.KeepAlive = KeepAlive
        ' oFTP.EnableSsl = UseSSL
        ' If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
        oFTP.UseBinary = True
        Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream
        Dim fs As New FileStream(DestFile, FileMode.Create)
        Dim buffer(2047) As Byte
        Dim read As Integer = 1
        While read <> 0
            read = responseStream.Read(buffer, 0, buffer.Length)
            fs.Write(buffer, 0, read)
        End While
        responseStream.Close()
        fs.Flush()
        fs.Close()
        responseStream.Close()
        response.Close()
        oFTP = Nothing
        Return True
    End Function

    Public Function ValidateServerCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
        If sslPolicyErrors = sslPolicyErrors.RemoteCertificateChainErrors Then
            Return False
        ElseIf sslPolicyErrors = sslPolicyErrors.RemoteCertificateNameMismatch Then
            Dim z As System.Security.Policy.Zone = System.Security.Policy.Zone.CreateFromUrl(CType(sender, HttpWebRequest).RequestUri.ToString)
            If z.SecurityZone = System.Security.SecurityZone.Intranet Or z.SecurityZone = System.Security.SecurityZone.MyComputer Then
                Return True
            End If
            Return False
        End If
        Return True
    End Function

//西蒙

1 个答案:

答案 0 :(得分:0)

您是收到错误消息还是不按预期执行操作?你正在创建一个FileStream并将其指向一个名为DESKTOP的文件,这是你真正打算做的,还是这只是一个例子?