【问题标题】:"The remote server returned an error: (501) Syntax error in parameters or arguments." when uploading TO FTP FROM Windows Azure“远程服务器返回错误:(501) 参数或参数中的语法错误。”从 Windows Azure 上传到 FTP 时
【发布时间】:2012-02-19 23:27:46
【问题描述】:

我需要不断地将生成的文件从 Azure 上传到客户端的 FTP,但是当我运行下面的代码时,它给了我...

远程服务器返回错误:(501) 参数或参数中的语法错误。

...在 Azure 模拟器中也可以正常工作。

这是一个概念证明目的草稿代码,所以我没有故意使用 Worker 角色、队列或 blob 等...

using System;
using System.IO;
using System.Net;
using System.Web;

namespace CloudWeb
{
    /// <summary>
    /// Summary description for Ftp
    /// </summary>
    public class Ftp : IHttpHandler
    {
        private const string FtpHost = "ftp://ftp.Host.com/App_Data/{0}";
        private const string FtpUserName = "UserName";
        private const string FtpPassword = "Password";
        private const string WarningImageFile = "images/status_warning.png";

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            UploadFtp(context.Server.MapPath(WarningImageFile));
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private static void UploadFtp(string source)
        {
            // read local file
            byte[] bFile = File.ReadAllBytes(source);

            // set ftp values
            var myFtp = (FtpWebRequest)WebRequest.Create(String.Format(FtpHost, Path.GetFileName(source)));
            myFtp.Method = WebRequestMethods.Ftp.UploadFile;
            myFtp.UsePassive = false;
            myFtp.UseBinary = true;
            myFtp.KeepAlive = true;
            myFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);

            // upload file
            using (Stream clsStream = myFtp.GetRequestStream())
            {
                clsStream.Write(bFile, 0, bFile.Length);
                clsStream.Close();
                //clsStream.Dispose();
            }

            // ReSharper disable RedundantAssignment
            myFtp = null;
            // ReSharper restore RedundantAssignment
        }
    }
}

【问题讨论】:

    标签: azure ftp


    【解决方案1】:

    如果您将 UsePassive 设置为 false,那么您需要确保命令通道的端口是打开的(即,您需要定义端点和访问规则)。除非有充分的理由不使用被动,否则最好使用被动。

    埃里克

    【讨论】:

    • 谢谢!许多旧的 FTP 服务器不支持 Passive,这就是我将其设置为 false 的原因。即使将其设置为 true 也解决了问题,是否有文章链接可以提供给我关于如何将其设置为端口的文章链接?
    • 被动模式已经存在了将近 20 年,所以它可能被支持,但没有打开。至于端口设置,客户端将 IP/端口发送到服务器。我不相信有办法用 .NET 客户端做到这一点,所以你可能需要自己购买或推出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多