Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to resolve operation timed out problem in c# .net windows service?

I am writing this code:-
C#
private void DownloadFile(string file)
       {

 FTPSettings.IP = "abc.com/xyz";
                FTPSettings.UserID = "xxxxx";
                FTPSettings.Password = "xxxxx";
                string uri = "ftp://" + FTPSettings.IP + "/" + "Testing" + "/" + file;
                Uri serverUri = new Uri(uri);
                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return;
                }
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + "Testing" + "/" + file));                                
                reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);                
                reqFTP.KeepAlive = false;                
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                                
                reqFTP.UseBinary = true;
                reqFTP.Proxy = null;                 
                reqFTP.UsePassive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream responseStream = response.GetResponseStream();
                FileStream writeStream = new FileStream(@"D:\ABC\XYZ\bin\" + file, FileMode.Create);
                
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);               
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }                
                writeStream.Close();
                response.Close();
}

My is getting error "Operation has timed out.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Updated 4-Jun-14 1:47am
v2
Comments
Herman<T>.Instance 4-Jun-14 7:55am    
on which line?
[no name] 4-Jun-14 8:07am    
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
After this
[no name] 4-Jun-14 8:08am    
Some processed and then error.
Suvabrata Roy 4-Jun-14 9:36am    
Its clearly mean that your FTP throwing the error Refer to this : http://forums.iis.net/t/1200768.aspx?getting+FTP+folder+error+the+operation+timed+out+after+changing+the+forest+funtional+level+from+Windows+2000+to+2003+on+the+Windows+2003+Server
gggustafson 4-Jun-14 15:09pm    
Why are you using Rebex rather than .Net?

1 solution

The following code was tested and did not encounter a timeout
C#
// ********************************************* download_file

/// <summary>
/// uses FTP to down load a file from a web location to a
/// local file
///
/// following discussions are based on
///
///     www.host.domain/path/file.ext
///
/// </summary>
/// <param name="host">
/// host server; in the above "www.server.domain"
/// </param>
/// <param name="path">
/// path within the host; in the above "path"
/// </param>
/// <param name="filename">
/// full file name, including extension, if applicable; in the
/// above "file.ext"
/// </param>
/// <param name="user_name">
/// username of user making request; if the request is
/// anonymous, supply either "anonymous" or a null or empty
/// string
/// </param>
/// <param name="password">
/// password of the user making the request; if the request is
/// anonymous, supply the email address of the user making the
/// request
/// </param>
/// <param name="target_path">
/// supply the fully qualified name of the target location on
/// the local machine; insure the location is writeable
/// </param>
void download_file ( string  host,
                     string  path,
                     string  filename,
                     string  user_name,
                     string  password,
                     string  target_path )
    {

    StringBuilder   sb = new StringBuilder ( );

    sb.Append ( "ftp://" );
    sb.Append ( host );
    sb.Append ( "/" );
    sb.Append ( path );
    sb.Append ( "/" );
    sb.Append ( filename );

    try
        {
        FtpWebRequest  request;

        request = ( FtpWebRequest ) WebRequest.Create (
                                        sb.ToString ( ) );
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        if ( String.IsNullOrEmpty ( user_name ) )
            {
            user_name = "anonymous";
            }
        request.Credentials = new NetworkCredential (
                                                user_name,
                                                password );

        using ( FtpWebResponse  response =
                        ( FtpWebResponse ) request.
                                           GetResponse ( ) )
            {
            using ( Stream rs = response.
                                GetResponseStream ( ) )
                {
                using ( StreamReader sr =
                                     new StreamReader ( rs ) )
                    {
                    File.WriteAllText ( target_path,
                                        sr.ReadToEnd ( ) );
                    }
                }
            MessageBox.Show (
                String.Format (
                    "Download Complete {0}status{0}{1}",
                    Environment.NewLine,
                    response.StatusDescription ) );
            }
        }
    catch ( Exception ex )
        {
        MessageBox.Show (
            String.Format (
                "Download Failed{0}{1}{2}",
                Environment.NewLine,
                ex.Message,
                ex.StackTrace ) );
        }
    }

The Usings were:
C#
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

Hope that helps.
 
Share this answer
 
Comments
[no name] 5-Jun-14 2:20am    
It's showing error:-
Could not find a part of the path 'D:\Vint3DWinApplicationWithWindowservice\Vint3DWinApplication\Vint3DWinApplication\bin\'.
My path is absolutely correct.
gggustafson 5-Jun-14 7:53am    
What is "showing error"? Your original code? My solution?

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