WebRequest超时异常c#

时间:2017-11-03 12:17:44

标签: c# stream webrequest

我有一个超时为5000毫秒的网络请求。我有这个,因为服务器可能不会给出任何回复。一切正常,但是当达到超时时,它会抛出异常" InternalException:系统错误。"我在下面发布我的代码,希望它有所帮助,谢谢!

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; }

我遇到的错误:

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.

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我同意@stuartd的评论。如果"一切正常",并且您的文件上传,但服务器实施并未遵循FTP标准并且响应不正确或根本没有响应,那么您唯一的事情就是可以做的是吞下超时异常,不要抛出,继续前进。

但是,不要捕获常规Exception,而是尝试捕获可以使用的最具体的异常类型(例如WebException),然后在此之后仍然捕获常规异常类型,以便您可以记录并报告其他错误。