开发者

Visual Basic: FTP Download

I have tried different codes and non of them works for downloading a file from example DriveHQ.

But i do manage to upload so the Host seems to be fine.

EDIT*

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.RemoteCertific开发者_如何转开发ateChainErrors 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

//Simon


Are you getting an error message or does it just not do what you expect it to do? You're creating a FileStream and pointing it to a file called DESKTOP, is this what you actually intended to do or was this just an example?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜