10

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

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

3 回答 3

14
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property
于 2013-10-21T15:59:30.397 回答
7

事实证明,配置代理时仅支持 、 和 方法RETRLIST并且您没有在代码中设置代理并不重要:如果在系统代理设置中配置了代理(不是代理)(在即:Internet Options\Connections\LAN setting\Proxy Server\ 为您的 LAN 使用代理服务器),然后在尝试上传到服务器时会出现此错误。NLSTSystem.Net.FtpWebRequestHTTPHTTPFTPFTP

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

request.Proxy = null;
于 2015-03-14T07:37:49.150 回答
0

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

于 2013-12-04T17:38:04.247 回答