简体   繁体   English

FTP上载文件使用HTTP代理时不支持请求的FTP命令

[英]FTP upload file The requested FTP command is not supported when using HTTP proxy

Can someone please take a look at the code below and tell me what I am doing wrong. 有人可以看看下面的代码并告诉我我做错了什么。 I am just going in circles,,, any pointers greatly appreciated 我只是在圈子里,任何指针都非常感激

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;
        }
    }
}
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property

It turns out that only the RETR , LIST , and NLST methods are supported by System.Net.FtpWebRequest when a HTTP proxy is configured and it doesn't matter that you are not setting a proxy in your code: if a HTTP proxy (not FTP proxy) is configured in the system proxy settings (in ie : Internet Options\\Connections\\LAN setting\\Proxy Server\\ Use a proxy server for your LAN), then you will get this error when trying to upload to the FTP server. 事实证明,当配置HTTP代理时, System.Net.FtpWebRequest仅支持RETRLISTNLST方法,并且您没有在代码中设置代理并不重要:如果是HTTP代理(不是FTP代理)在系统代理设置中配置(即:Internet选项\\连接\\ LAN设置\\代理服务器\\使用LAN的代理服务器),然后在尝试上传到FTP服务器时会出现此错误。

The workaround is use IE to change the system settings to switch off the use of the HTTP proxy. 解决方法是使用IE更改系统设置以关闭HTTP代理的使用。 However if you have access to the affected code the solution is to set the Proxy property of the request to null, for example: 但是,如果您可以访问受影响的代码,则解决方案是将请求的Proxy属性设置为null,例如:

request.Proxy = null;

The exceptions itself is the answer - it is not supported. 例外本身就是答案 - 它不受支持。 Probably you have some HTTP proxy that is preventing direct connection to FTP. 可能你有一些HTTP代理阻止直接连接到FTP。 According to MS documentation , if the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported - so UploadFile is not. 根据MS文档 ,如果指定的代理是HTTP代理,则仅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令 - 因此UploadFile不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM