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

本文关键字:FTP 不支持 请求 命令 代理 文件 使用 HTTP | 更新日期: 2023-09-27 18:36:20

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

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;
        }
    }
}

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

req.Proxy = new WebProxy(); // initialize this FtpWebRequest property

事实证明,当配置HTTP代理时,System.Net.FtpWebRequest仅支持 RETRLISTNLST 方法,并且您在代码中没有设置代理并不重要: 如果在系统代理设置中配置了HTTP代理(不是FTP代理)(即: 互联网选项''连接''局域网设置''代理服务器''为局域网使用代理服务器),则在尝试上传到FTP服务器时会收到此错误。

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

request.Proxy = null;

例外本身就是答案 - 它不受支持。可能您有一些阻止直接连接到FTP的HTTP代理。根据 MS 文档,如果指定的代理是 HTTP 代理,则仅支持 DownloadFile、ListDirectory 和 ListDirectoryDetails 命令 - 因此 UploadFile 不受支持。