Ftp throws WebException only in .NET 4.0

浪子不回头ぞ 提交于 2019-12-12 19:59:22

问题


I have following c# code. It runs just fine when compiled against .NET framework 3.5 or 2.0 (I did not test it against 3.0, but it will most likely work too). The problem is, that it fails when built against .NET framework 4.0.

        FtpWebRequest Ftp = (FtpWebRequest)FtpWebRequest.Create(Url_ + '/' + e.Name);

        Ftp.Method = WebRequestMethods.Ftp.UploadFile;
        Ftp.Credentials = new NetworkCredential(Login_, Password_);
        Ftp.UseBinary = true;
        Ftp.KeepAlive = false;
        Ftp.UsePassive = true;

        Stream S = Ftp.GetRequestStream();

        byte[] Content = null;
        bool Continue = false;
        do
        {
            try
            {
                Continue = false;
                Content = File.ReadAllBytes(e.FullPath);
            }
            catch (Exception)
            {
                Continue = true;
                System.Threading.Thread.Sleep(500);
            }
        } while (Continue);

        S.Write(Content, 0, Content.Length);
        S.Close();

        FtpWebResponse Resp = (FtpWebResponse)Ftp.GetResponse();
        if (Resp.StatusCode != FtpStatusCode.CommandOK)
            Console.WriteLine(Resp.StatusDescription);

The problem is in call Stream S = Ftp.GetRequestStream();, which throws an en instance of WebException with message “The remote server returned an error: (500) Syntax error, command unrecognized”.

Does anybody know why it is so?

PS. I communicate with virtual ftp server in ECM Alfresco.

edit: I recently found out, that it is not possible to have .NET framework 3.5 windows service on Windows 7, so it would be nice to solve this issue. The thing I need to do is upload file to ftp - is there another (working) way in c#?

edit2: previous edit is not true, I got somewhat confused ...


回答1:


I found a similar post here http://bytes.com/topic/net/answers/792209-problems-ftpwebrequest-getrequeststream-c

with a resolution that worked for that person.

try this:

change

reqFTP.UsePassive = true;

to

reqFTP.UsePassive = false;

UsePassive: Specifies whether to use either active or passive mode. Earlier, active FTP worked fine with all clients, but now, as most of the random ports are blocked by a firewall, the active mode may fail. The passive FTP is helpful in this case. But still, it causes issues at the server. The higher ports requested by client on server may also be blocked by a firewall. But, because FTP servers will need to make their servers accessible to the greatest number of clients, they will almost certainly need to support passive FTP. Passive mode is considered safe because it ensures all data flow initiation comes from inside (client) the network rather than from the outside (server).

Also, per the MSDN Documentation at http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usepassive.aspx

If UsePassive is set to true, the FTP server may not send the size of the file, and download progress can always be zero. If UsePassive is set to false, a firewall can raise an alert and block the file download.




回答2:


I had similar problem. I am using FTP.Net 2.0 and my application is compiled on FW4.0 target. I solved it by adding a property in the app.config file :

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

More Information here : What 'additional configuration' is necessary to reference a .NET 2.0 mixed mode assembly in a .NET 4.0 project?



来源:https://stackoverflow.com/questions/4604693/ftp-throws-webexception-only-in-net-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!