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

C# FTP上载文件使用HTTP代理时不支持请求的FTP命令,c#,ftp,ftpwebrequest,C#,Ftp,Ftpwebrequest,请有人看一下下面的代码,告诉我我做错了什么。我只是在兜圈子,非常感谢任何指点 public class FtpWebRequestUtil { private static string RemoteHost; private static string RemoteFtpPath; public static NetworkCredential Credential = new NetworkCredential(); public FtpWebRequest

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

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。根据,如果指定的代理是HTTP代理,则仅支持DownloadFile、ListDirectory和ListDirectoryDetails命令,因此UploadFile不受支持。

结果表明,只有
RETR
列表
,当配置了
HTTP
代理并且您没有在代码中设置代理并不重要时,
System.Net.FtpWebRequest
支持
NLST
方法:如果在系统代理设置中配置了
HTTP
代理(而不是
FTP
代理)(即:Internet选项\Connections\LAN设置\Proxy Server\Use a Proxy Server for your LAN),则在尝试上载到
FTP
服务器时,会出现此错误

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

request.Proxy = null;

ftp命令在使用http proxyok时不受支持(尝试过),现在获取对象引用未设置为实例):您在哪里添加了这行代码?使用changesreq.Proxy=newWebProxy()发布代码;req.Proxy=null;请求凭证=凭证;//FtpWebRequest req=(FtpWe req.UseBinary=true;req.useAssiposal=true;req.KeepAlive=true;StreamWriter myStreamWriter=new StreamWriter(req.GetRequestStream());由于字符限制,不允许发布完整代码。您可以在帖子中编辑代码。无论如何-remove req.Proxy=null;您从何处获得此对象引用错误?在使用对象之前,您一定没有初始化过它。答案应该是正确的,解释也是+1。
request.Proxy = null;