2011-03-15 27 views
0

我已經嘗試過不同的代碼,並且它們都不適用於從示例DriveHQ下載文件。Visual Basic:FTP下載

但我設法上傳,所以主機似乎很好。

編輯*

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 

//西蒙

+2

你可以發佈一些你已經試過的代碼,所以我們可以引導你在正確的方向嗎? – 2011-03-15 21:24:26

+0

在這裏你去! – user564612 2011-03-15 21:35:25

+0

@carmstrong告訴我,如果你得到它的權利 – user564612 2011-03-15 21:37:22

回答

0

您是否收到錯誤信息或它只是不會做你希望它做什麼?你正在創建一個FileStream並將其指向一個名爲DESKTOP的文件,這是你實際打算做的還是僅僅是一個例子?

+0

我得到一個運行時錯誤。錯誤530:你沒有登錄,但我是:( – user564612 2011-03-16 15:05:05

+0

你的代碼是有效的,我只是用一個有效的用戶名和密碼對我的FTP服務器進行了測試。'(530)未登錄'表示無效的用戶名/密碼 – 2011-03-16 15:32:21

+0

@Chris -Haas我確定我有一個有效的用戶名和密碼,因爲我可以在網站 – user564612 2011-03-18 19:50:15