2016-11-28 80 views
0

我必須在使用TLS和我的C#應用​​程序(.net 3.5) 的FTP上傳一些文件使用Filezila,沒有問題。使用FtpWebRequest和SSL時出現超時錯誤

現在,用我的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。

任何人有一些想法嗎?

非常感謝,

+0

顯示我們一個確切的異常消息和它的調用堆棧(甚至更好的[的FtpWebRequest日誌](HTTP:/ /stackoverflow.com/q/9664650/850848))+發佈FileZilla日誌文件。 –

+0

Oo我在我的主機上犯了一個錯誤...我使用FTP://所以URL是FTP:// FTP:/ xxxxx。順便說一句,糾正後,我有另一個異常;:「」遠程服務器返回一個錯誤:234 AUTH TLS OK。\ r \ n。'' –

+0

然後我們需要[FtpWebRequest日誌](http:// stackoverflow。 com/q/9664650/850848)+編輯您的問題標題和文本以獲取新的例外情況 –

回答

0

我不停的FTP://宿主,所以網址爲FTP://FTP:/xxxxx