“远程服务器返回错误:(501) 参数或参数中存在语法错误。”从 Windows Azure 上传至 FTP 时
我需要不断地将生成的文件从 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
}
}
}
I need to continuously upload a generated file from Azure to client's FTP but when I run the code below it gives me ...
The remote server returned an error: (501) Syntax error in parameters or arguments.
...also in Azure emulator it works fine.
This is a proof of concept purposes draft code, so I did not use Worker role, queue or blob etc. intentionally...
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
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将 UsePassive 设置为 false,则需要确保命令通道的端口已打开(即,需要定义端点和访问规则)。除非有充分的理由不使用被动,否则使用被动会更好。
埃里克
If you set UsePassive to false, then you need to make sure that the port for the command channel is open (i.e., you need to define endpoints and access rules). Unless there is a good reason to not use passive, you are far better off using passive.
Erick