Update (cause of this problem):
Well, I was able - with the help of @MitchelSellers comment - to find out exactly where the problem is, and I'm trying to find a solution for it.
- Obviously, this server does not allow the command
CWD(change working directory). - Now downloading the file using
.NET 2.0for unknown reasons, it sends this command /after logging in to change the working directory to the current working directory! (as as shown below, and I also confirmed that using FileZilla with another server). - Also, as shown below, this does not apply to the latest versions
.NET, so 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 is 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 this code on .NET 4or 4.xit works fine. However, when running on .NET 2.0(I should use .NET 2.0), it throws an exception when called request.GetResponse. Here's the exception message:
The remote server returned an error: (501) Syntax error in parameters or arguments.
I realized that there .NET 2.0must be something wrong with the request sent from , so I decided to capture the requests and responses using Wireshark, and my guess was correctbut I still don’t understand that the problem is that I use the same code for both versions .NET.
Wireshark Results
.NET 4.5.2

.NET 2.0

Any ideas?
: VB, # .