FTP抛出WebException只能在.NET 4.0中 [英] Ftp throws WebException only in .NET 4.0

查看:254
本文介绍了FTP抛出WebException只能在.NET 4.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C#code。这时候在.NET framework 3.5或2.0编译运行得很好(我不反对3.0测试,但它很可能会工作得)。现在的问题是,它在针对.NET Framework 4.0中内置失败。

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

        Ftp.Method = WebRequestMethods.Ftp.UploadFile;
        Ftp.Credentials =新的NetworkCredential(Login_,Password_);
        Ftp.UseBinary = TRUE;
        Ftp.KeepAlive = FALSE;
        Ftp.UsePassive = TRUE;

        流S = Ftp.GetRequestStream();

        byte []的内容= NULL;
        布尔继续= FALSE;
        做
        {
            尝试
            {
                继续= FALSE;
                内容= File.ReadAllBytes(e.FullPath);
            }
            赶上(例外)
            {
                继续= TRUE;
                System.Threading.Thread.Sleep(500);
            }
        }而(继续);

        S.Write(内容,0,Content.Length);
        S.CLOSE();

        FtpWebResponse反应谱=(FtpWebResponse)Ftp.GetResponse();
        如果(Resp.Status code!= FtpStatus code.CommandOK)
            Console.WriteLine(Resp.StatusDescription);
 

现在的问题是在调用流的 S = Ftp.GetRequestStream(); 的,它抛出一个连接WebException有消息的情况下远程服务器返回错误:(500)语法错误,不可识别的命令

是否有人知道为什么会这样?

PS。我在ECM露天虚拟FTP服务器进行通信。

编辑:我最近发现,这是不可能有.NET Framework 3.5的Windows服务在Windows 7上,所以这将是很好的解决这个问题。我需要做的事情是上传文件到FTP? - 是有在C#中的另一个(工作)的方式

EDIT2:previous编辑是不是真的,我有点糊涂了......

解决方案

我发现了一个类似的帖子这里的http://bytes.com/topic/net/answers/792209-problems-ftpwebrequest-getrequeststream-c

与工作的那个人的分辨率。

试试这个:

变更

  reqFTP.UsePassive = TRUE;
 

  reqFTP.UsePassive = FALSE;
 

  

UsePassive:指定是否使用   无论是主动还是被动模式。   此前,主动FTP工作得很好用   所有客户端,但现在,因为大多数的   随机端口被阻止通过   防火墙,主动模式可能会失败。   被动FTP是在这个有帮助   外壳。但尽管如此,它会导致问题在   服务器。较高的端口要求   由客户端上的服务器也可以是   防火墙阻止。但是因为   FTP服务器需要让自己的   访问的最大的服务器   客户的数量,他们几乎   必须得支持被动FTP。   被动模式被认为是安全的   因为它保证所有的数据流   引发来自内部(客户端)   网络而不是从   外(服务器)。

此外,每MSDN文档在 的http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usepassive.aspx

  

如果UsePassive设置为true,则FTP   服务器可能无法发送的大小   文件,并下载进度可以随时   是零。如果UsePassive设置为   假的,防火墙可以发出警报   并阻止文件下载。

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 ...

解决方案

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.

这篇关于FTP抛出WebException只能在.NET 4.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆