VB.NET - Download a file on a UNIX FTP Server with VB NET

Asked By Nicolas Zenou
24-Jul-06 10:23 AM
Hye,

I m trying to download a file on a UNIX FTP server on a VB.NET application, but I always get the message : 

"The remote server returned an error : (503) bad sequence of commands."

I am actually using this piece code: 

Dim downloadRequest As FtpWebRequest = CType(WebRequest.Create(myUri), FtpWebRequest)

downloadRequest.UsePassive = False
     
 Dim downloadResponse As FtpWebResponse = CType(downloadRequest.GetResponse, FtpWebResponse)
myResponseStream = downloadResponse.GetResponseStream()


And its raising the exception when I launch de GetResponseStream() method.

Does anyone know why it's not working with a UNIX server while it's work on a WINDOWS FTP server ?

Use FtpWebRequest to download a file  Use FtpWebRequest to download a file

24-Jul-06 10:31 AM
Reason for ur error:
 This response indicates that a remote SMTP Server or client is attempting to conduct a series of SMTP transactions out of sequence. 


Alternate Code:
=========

<PRE lang=vbnet>'Values to use
Const localFile As String = "C:\myfile.bin"
Const remoteFile As String = "/pub/myftpfile.bin"
Const host As String = "ftp://ftp.myhost.com"
Const username As String = "myuserid"
Const password As String = "mypassword"

'1. Create a request: must be in ftp://hostname format, 
'   not just ftp.myhost.com
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = _
    CType(FtpWebRequest.Create(URI), FtpWebRequest)

'2. Set credentials
ftp.Credentials = New _
    System.Net.NetworkCredential(username, password)

'3. Settings and action
ftp.KeepAlive = False
'we want a binary transfer, not textual data
ftp.UseBinary = True
'Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

'4. If we were using a method that uploads data e.g. UploadFile
'   we would open the ftp.GetRequestStream here an send the data

'5. Get the response to the Ftp request and the associated stream
Using response As System.Net.FtpWebResponse = _
      CType(ftp.GetResponse, System.Net.FtpWebResponse)
  Using responseStream As IO.Stream = response.GetResponseStream
    'loop to read & write to file
    Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
      Dim buffer(2047) As Byte
      Dim read As Integer = 0
      Do
        read = responseStream.Read(buffer, 0, buffer.Length)
        fs.Write(buffer, 0, read)
      Loop Until read = 0 'see Note(1)
      responseStream.Close()
      fs.Flush()
      fs.Close()
    End Using
    responseStream.Close()
  End Using
  response.Close()
End Using

'6. Done! the Close happens because ftp goes out of scope
'   There is no .Close or .Dispose for FtpWebRequest</PRE>

http://www.codeproject.com/vb/net/FtpClient.asp

HTH

that's ok now!  that's ok now!

24-Jul-06 10:55 AM
Thanks for this one.
These two lines saved me:

ftp.KeepAlive = False 
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile 

And now it works fine.

Thanks.
Create New Account
help
string [] GetFileList() { string [] downloadFiles; StringBuilder result = new StringBuilder(); WebResponse response = null ; StreamReader reader = null ; try { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create( new Uri( "ftp: / / " + ftpServerIP + " / " )); reqFTP.UseBinary = true ; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; reqFTP.Proxy = null ; reqFTP.KeepAlive = false ; reqFTP.UsePassive = false ; response = reqFTP.GetResponse(); reader new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line ! = null ) { result.Append(line); result.Append( " \ n" ); line = reader ftp: / / " + ftpServerIP + " / " + remoteDir + " / " + file; Uri serverUri = new Uri(uri); if (serverUri.Scheme ! = Uri.UriSchemeFtp) { return ; } FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create( new Uri( "ftp: / / " + ftpServerIP + " / " + remoteDir + " / " + file)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP
class WebRequestGetExample { public static void Main () { / / Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create( " ftp: / / www.contoso.com / " ); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; / / This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ( "anonymous" , "janeDoe@contoso.com FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine( "Directory List Complete, status list help me, listBox1.Items.Clear(); listBox2.Items.Clear(); string [] downloadFiles; StringBuilder result = new StringBuilder (); FtpWebRequest reqFTP; try { reqFTP = ( FtpWebRequest ) FtpWebRequest .Create( new Uri ( "ftp: / / " + textBox2.Text.Trim() + " / " )); reqFTP.UsePassive = false
files from a remote web server. I have written the following code snippet for downloading. FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp: / / xxx.xx.xxx.xx / Test / "); request.Credentials = new NetworkCredential("user", "pwd"); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response GetResponseStream(); But after executing "FtpWebResponse response = (FtpWebResponse)request.GetResponse();" statement the following error is coming "The requested URI is invalid
returns> public static FileProcessResult DownloadFile( string ftpFileURI, string localFilePath, string ftpUserID, string ftpPassword, bool deleteFileAfterDownload) { FtpWebRequest ftpWebRequest = null ; FileProcessResult processResult = null ; try { processResult = new FileProcessResult() { FileName = Path.GetFileName(localFilePath), ProcessResultType = ProcessResultType.Success Path.GetDirectoryName(localFilePath))) { processResult.ProcessResultType = ProcessResultType.Failure; processResult.Message = "Invalid local file path." ; return processResult; } ftpWebRequest = FtpWebRequest.Create(ftpFileURI) as FtpWebRequest; ftpWebRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpWebRequest.KeepAlive = false ; ftpWebRequest.UseBinary = true ; using (FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest
a Text File To download a text file using FTP: • Create an instance of the FtpWebRequest class using the Create() method of the WebRequest class. The Create() method takes in a the command to be sent to the FTP server using the Method property of the FtpWebRequest class; in this case this command is DownloadFile . • Specify the login credential to the FTP server. • Obtain the response from the FTP server using the GetResponse() method from the FtpWebRequest class. • Retrieve the stream that contains response data sent from an FTP server using the GetResponseStream() method from the FtpWebResponse class. Note that you can use a StreamReader object to read the content of the text file: Try Dim filename As String = ftpURI & "test.txt" Dim ftpReq As FtpWebRequest = WebRequest.Create(filename) ftpReq.Method = WebRequestMethods.Ftp.DownloadFile ftpReq.Credentials = New NetworkCredential("anonymous", "password") Dim ftpResp As FtpWebResponse = ftpReq.GetResponse Dim ftpRespStream As Stream = FTPResp.GetResponseStream Dim