Uploading an FTP file from a (specific) server works on .NET 4+, but does not work on .NET 2.0

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

Wireshark Results & lt;  code> .NET 4.5.2 </code>

.NET 2.0

Wireshark Results & lt;  code> .NET 2.0 </code>

Any ideas?

: VB, # .

+4
2

UPDATE :

.NET 2 :-( BuildCommandsList() - .NET 2 :

if (m_PreviousServerPath != newServerPath) { 
    if (!m_IsRootPath
        && m_LoginState == FtpLoginState.LoggedIn
        && m_LoginDirectory != null)
    { 
        newServerPath = m_LoginDirectory+newServerPath;
    } 
    m_NewServerPath = newServerPath; 

    commandList.Add(new PipelineEntry(FormatFtpCommand("CWD", newServerPath), PipelineEntryFlags.UserCommand)); 
}

, CWD .NET 2. 2 "": RFC- FTP- ( ), - / .

/ FTP- DOS .NET...

:

MS .NET 2 .NET 4 : https://support.microsoft.com/en-au/help/2134299/system.net.ftpwebrequest-class-behaves-differently-in-.net-framework-4-vs-.net-framework-3.5

, .NET 2.0 - - :

private static void SetMethodRequiresCWD()
{
        Type requestType = typeof(FtpWebRequest);
        FieldInfo methodInfoField = requestType.GetField("m_MethodInfo", BindingFlags.NonPublic | BindingFlags.Instance);
        Type methodInfoType = methodInfoField.FieldType;


        FieldInfo knownMethodsField = methodInfoType.GetField("KnownMethodInfo", BindingFlags.Static | BindingFlags.NonPublic);
        Array knownMethodsArray = (Array)knownMethodsField.GetValue(null);

        FieldInfo flagsField = methodInfoType.GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance);

        int MustChangeWorkingDirectoryToPath = 0x100;
        foreach (object knownMethod in knownMethodsArray)
        {
            int flags = (int)flagsField.GetValue(knownMethod);
            flags &= (~MustChangeWorkingDirectoryToPath);
            flagsField.SetValue(knownMethod, flags);
        }
}

- , ...

EDIT: ftp, , .NET 4.

+1

filePath ? iee

Dim filePath As String = "ftp://ftp.kohlandfrisch.com/%2ftestfile"

% 2f /.

.

0

Source: https://habr.com/ru/post/1667799/


All Articles