【问题标题】:I get a timeout error when using FtpWebRequest with SSL将 FtpWebRequest 与 SSL 一起使用时出现超时错误
【发布时间】:2016-11-28 15:24:06
【问题描述】:

我必须在我的 C# 应用程序 (.net 3.5) 中使用 TLS 的 FTP 上上传一些文件 使用 Filezila,没有问题。

现在,使用我的 C# 代码,我有一个超时异常,我真的不知道为什么,因为 Filezila 一切正常。

这是我的代码:

    public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

    public static string Upload_SSL(string filenameSrc)
    {

        ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;

        FileInfo fileInfSrc = new FileInfo(filenameSrc);
        FtpWebRequest reqFTP;

        // Create FtpWebRequest object from the Uri provided
        if (strDirectory.Trim() != "")
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + fileInfSrc.Name.Trim()));
        }
        else
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + fileInfSrc.Name.Trim()));
        }

        reqFTP.EnableSsl = true;

        // Provide the WebPermission Credintials
        reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());

        reqFTP.Proxy = null;

        // By default KeepAlive is true, where the control connection is not closed
        // after a command is executed.
        reqFTP.KeepAlive = false;

        // Specify the command to be executed.
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

        // Specify the data transfer type.
        reqFTP.UseBinary = true;

        // Notify the server about the size of the uploaded file
        reqFTP.ContentLength = fileInfSrc.Length;

        // The buffer size is set to 8kb
        int buffLength = 8192;
        byte[] buff = new byte[buffLength];
        int contentLen;

        // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
        FileStream fs = fileInfSrc.OpenRead();

        try
        {
            // Stream to which the file to be upload is written
            Stream strm = reqFTP.GetRequestStream();

            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);


            // Till Stream content ends
            while (contentLen != 0)
            {
                // Write Content from the file stream to the FTP Upload Stream
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            // Close the file stream and the Request Stream
            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            fs.Close();
            return (ex.Message);
        }

        return "ok";
    }

现在,来电:

        myFtp.Class1.strHost = "ftp://XXXXXXXXXXXXX";
        myFtp.Class1.strPass = "*****************";
        myFtp.Class1.strUser = "*********";
        myFtp.Class1.nPort = 21;
        myFtp.Class1.Upload_SSL(@"D:\Test.txt");

作为信息,如果我尝试使用我的代码在非 TLS/SLL 服务器上上传文件,一切正常。所以问题似乎只针对 TLS/SSL ftp。

有人有什么想法吗?

非常感谢,

【问题讨论】:

  • 向我们展示确切的异常消息及其调用堆栈(或者更好的是 FtpWebRequest log)+ 发布 FileZilla 日志文件。
  • 哦,我在主机上犯了一个错误...我保留了 FTP://,所以 URL 是 FTP://FTP:/xxxxx。顺便说一句,更正后我有另一个异常;:“”远程服务器返回错误:234 AUTH TLS OK。\r\n。”'
  • 那么我们需要FtpWebRequest log + 编辑您的问题标题和文本以获取新例外。
  • 我现在创建一个新问题

标签: c# .net visual-studio ssl ftp


【解决方案1】:

我在主机中保留了FTP://,所以网址是FTP://FTP:/xxxxx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 2015-07-15
    • 1970-01-01
    • 2019-06-26
    • 2021-06-25
    • 2018-06-16
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多