Update (the reason of this issue):
Okay, I managed -with the help of @MitchelSellers's comment- to figure out where the problem exactly is, and I'm trying to find a solution for it.
- Obviously this server doesn't allow the command
CWD
(change working directory). - Now downloading a file using
.NET 2.0
-for unknown reasons- it sends this command followed by/
after logging in, to change the working directory to the current working directory!! (as shown below, and I also confirmed that using FileZilla with another server). - Also as shown below, that's not the case with the recent versions of
.NET
, which is why it works on.NET 4.x
.
So, I'm trying to find a way to make it work on .NET 2.0
.
The original question:
Here's my code:
Private Sub DownloadTestFile()
Dim filePath As String = "ftp://ftp.kohlandfrisch.com/testfile"
Dim request As FtpWebRequest
Dim buffer(1023) As Byte
Dim bytesIn As Integer
request = DirectCast(WebRequest.Create(filePath), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("username", "password")
request.UseBinary = False
request.UsePassive = True
request.Proxy = Nothing
Using stream As IO.Stream = request.GetResponse.GetResponseStream
Using output = IO.File.Create(localFilePath)
bytesIn = 1
Do Until bytesIn < 1
bytesIn = stream.Read(buffer, 0, 1024)
If bytesIn > 0 Then output.Write(buffer, 0, bytesIn)
Loop
End Using
End Using
End Sub
When running that code on .NET 4
or 4.x
, it works perfectly fine. However when running it on .NET 2.0
(I have to use .NET 2.0), it throws an exception when calling request.GetResponse
. Here's the exception message is:
The remote server returned an error: (501) Syntax error in parameters or arguments.
I figured out that there must be something wrong with the request sent from .NET 2.0
, so I decided to capture requests and responses using Wireshark, and my assumption was correct, but I still don't understand what the problem exactly is since I'm using the same code on both ..NET
versions
Wireshark results
.NET 4.5.2
.NET 2.0
Any ideas?
Side note: Although my code is in VB, any answer with C# code is welcomed.