Reputation: 664
I have a web request with a timeout of 5000 milliseconds. I have this because the server probably could don't give any response. All works fine, but when the timeout is reached, it throws me an exception "InternalException: System error." I post my code below, hope it helps, thanks!
try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://xxxx.com/xxx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.EnableSsl = true;
request.Credentials = new NetworkCredential("xxxx@xxxx", "xxxxxxx");
ServicePointManager.ServerCertificateValidationCallback = delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return (true);
};
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FileStream stream = File.OpenRead("C:\\xxxx");
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
request.Timeout = 5000;
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
actBarLenght = buffer.Length;
reqStream.Flush();
reqStream.Close();
File.Delete("H:\\xxxxx");
uploading = false;
}
catch (Exception ex) { throw ex; }
The error I have:
System.Net.WebException occurred
HResult=0x80131509
Message=System error.
Source=ConfigurationApp
StackTrace:
at ConfigurationApp.SysTimeDate.ftpSend() in C:\xxx\SysTimeDate.cs:line 2177
at xxx.frmConfiguration.button30_Click(Object sender, EventArgs e) in C:\xxx\frmConfiguration.cs:line 3583
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ConfigurationApp.Program.Main() in C:\xxx\Program.cs:line 20
Inner Exception 1:
InternalException: System error.
Can anyone help me?
Upvotes: 1
Views: 637
Reputation: 1328
I agree with @stuartd from comments. If "all works fine", and your file is uploaded, but the server implementation doesn't follow FTP standards and responds incorrectly or not at all, then the only thing you can do is to swallow the timeout exception, don't throw, and move on.
However, instead of catching the general Exception
, try and catch the most specific exception type that you can (e.g. WebException
), and then still catch the general exception type after that so that you can log and report on other errors.
Upvotes: 1