Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
When i try to connect to FTP using c sharp code i got time out expired error on below mentioned line.
response = (FtpWebResponse)request.GetResponse();
C#
below is all my code which i use to connect to FTP using SSL.

{
            FtpWebResponse response = null;
            string str = info.DomainName.Trim().ToString() + ":" + info.Port;
            string username = info.Username;
            string password = info.Password;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + str + "/" + dirName));

            try
            {
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential(username, password);
                request.KeepAlive = info.KeepAlive;
                request.Timeout = info.TimeOut;//900000
                request.ReadWriteTimeout = info.TimeOut;
                request.UsePassive = (!info.UsePassive);
                request.Proxy = null;
                request.EnableSsl = true;

                RemoteCertificateValidationCallback orgCallback = ServicePointManager.ServerCertificateValidationCallback;
                try
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidateCertificate);
                    ServicePointManager.Expect100Continue = true;

                    response = (FtpWebResponse)request.GetResponse();
                    if (response.StatusCode.ToString() == "OpeningData")
                    {
                        response.Close();
                        return true;
                    }
                }
                finally
                {
                    ServicePointManager.ServerCertificateValidationCallback = orgCallback;
                }
               
            }
            catch (WebException ex)
            {
                if (request != null)
                {
                    request.Abort();
                }
                throw ex;
            }
            catch (Exception exx)
            {
                response.Close();
                throw exx;
            }
            return true;
        
}


Please help! thanks in advance.
Posted
Updated 17-Nov-13 21:42pm
v3

1 solution

You may want to try to configure network tracing to see what is going on:

How to configure network tracing[^]

If you remove the sharedListeners section, the output will go directly to the output window. You add the above configuration in the App.Config file.

Also, there is more information in the WebException about why the timeout happened, try breaking inside the catch and view the exception details in the debugger.
 
Share this answer
 
Comments
Farrukh Hayat Abbasi 15-Nov-13 8:59am    
As i exception details inner exception segment is null so i am unable to get more details it only shows me the settings which i have given.
Farrukh Hayat Abbasi 15-Nov-13 9:04am    
i have also configured network tracing but still the result is same.
Ron Beyer 15-Nov-13 9:10am    
Enabling network tracing won't fix the issue, but it should give you more insight, what did the logs say was happening?
Farrukh Hayat Abbasi 16-Nov-13 2:26am    
Can't get much details in log details. as i said inner exception, and most of the parameters are null. I think this is because as i don't get any response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900