【问题标题】:FTP upload file The requested FTP command is not supported when using HTTP proxyFTP 上传文件 使用 HTTP 代理时不支持请求的 FTP 命令
【发布时间】:2013-10-30 03:48:57
【问题描述】:

有人可以看看下面的代码并告诉我我做错了什么。我只是绕圈子,非常感谢任何指针

public class FtpWebRequestUtil
{
    private static string RemoteHost;
    private static string RemoteFtpPath;
    public static NetworkCredential Credential = new NetworkCredential();

    public FtpWebRequestUtil()
    {
    }

    public FtpWebRequestUtil(string RemoteAddress, string RemotePath, string RemoteUser, string RemotePwd)
    {
        Credential.UserName = RemoteUser;
        Credential.Password = RemotePwd;
        RemoteHost = RemoteAddress;
        RemoteFtpPath = RemotePath;
    } 

    public string UploadFile(string localFilePath)
    {
        int startTime = Environment.TickCount;
       // Console.WriteLine("Uploading File " + localFilePath);
        try
        {
            FileInfo localFile = new FileInfo(localFilePath); //e.g.: c:\\Test.txt
            byte[] buf = new byte[2048];
            int iWork;
            string remoteFile = "ftp://" + RemoteHost + "/" + RemoteFtpPath + "/" + localFile.Name;

            FtpWebRequest req = (FtpWebRequest) FtpWebRequest.Create(remoteFile);
           // req.Proxy = 

            req.Credentials = Credential;


           // FtpWebRequest req = (FtpWe

            req.UseBinary = true;
            req.KeepAlive = true;
            req.Method = WebRequestMethods.Ftp.UploadFile;
            StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream());
            myStreamWriter.Write(new StreamReader("TestFiles\\" + localFile.Name).ReadToEnd());
            myStreamWriter.Close();
            FtpWebResponse myFtpWebResponse = (FtpWebResponse) req.GetResponse();
            Console.WriteLine("Upload File Complete, status: " + myFtpWebResponse.StatusDescription);

            myFtpWebResponse.Close();
            return "SUCCESS";
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error connecting to the FTP Server.");
            Console.WriteLine(ex.Message);
            throw ex;
        }
        Console.WriteLine("Time taken for downloading file is " + (Environment.TickCount - startTime).ToString());
        return "FAILURE";
    }


    ************************                       *********************************
    FtpWebRequestUtil ftpClient = new FtpWebRequestUtil(FtpUrl, InputFolder, FtpUser, FtpPassword);
    try
    {
        Thread.Sleep(5000);
        ftpClient.UploadFile(UploadingFileName);
                }
        catch (Exception exception)
        {
            Assert.Fail(exception.Message);
        }
        finally
        {
            ftpClient = null;
        }
    }
}

【问题讨论】:

  • 使用 http 代理时不支持 ftp 命令

标签: c# ftp ftpwebrequest


【解决方案1】:

事实证明,当配置了HTTP 代理并且您没有在您的代码:如果在系统代理设置中配置了HTTP 代理(不是FTP 代理)(即:Internet Options\Connections\LAN setting\Proxy Server\ Use a proxy server for your LAN),那么您将尝试上传到FTP 服务器时出现此错误。

解决方法是使用 IE 更改系统设置以关闭 HTTP 代理的使用。但是,如果您有权访问受影响的代码,解决方案是将请求的 Proxy 属性设置为 null,例如:

request.Proxy = null;

【讨论】:

  • 应该是正确的答案,+1的解释。
【解决方案2】:
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property

【讨论】:

  • ok 试过了,现在获取对象引用未设置为实例):
  • 这行代码你在哪里添加的?发布带有更改的代码
  • req.Proxy = new WebProxy(); req.Proxy = null; req.Credentials = 凭据; // FtpWebRequest req = (FtpWe req.UseBinary = true; req.UsePassive = true; req.KeepAlive = true; StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream());
  • 由于字符限制,不允许发布完整代码
  • 您可以在帖子本身中编辑代码。无论如何 - 删除 req.Proxy = null;你从哪里得到这个对象引用错误?您必须在使用对象之前没有初始化它
【解决方案3】:

异常本身就是答案 - 它不受支持。可能您有一些 HTTP 代理阻止直接连接到 FTP。根据MS documentation,如果指定的代理是HTTP代理,则只支持DownloadFile、ListDirectory和ListDirectoryDe​​tails命令——所以UploadFile不支持。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2014-09-11
    相关资源
    最近更新 更多