WebRequest超时异常c#
WebRequest timeout exception c#
我有一个超时为 5000 毫秒的 Web 请求。我有这个是因为服务器可能不会给出任何响应。一切正常,但是当达到超时时,它会抛出一个异常 "InternalException: System error." 我 post 下面是我的代码,希望对您有所帮助,谢谢!
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.
谁能帮帮我?
我同意@stuartd 的评论。如果 "all works fine",并且您的文件 已 上传,但服务器实现不遵循 FTP 标准并且响应不正确或根本不响应,那么您唯一要做的就是可以做的是吞下超时异常,不要抛出,继续。
但是,与其捕获一般的 Exception
,不如尝试捕获最具体的异常类型(例如 WebException
),然后仍然捕获一般的异常类型,以便您可以记录并报告 其他 错误。
我有一个超时为 5000 毫秒的 Web 请求。我有这个是因为服务器可能不会给出任何响应。一切正常,但是当达到超时时,它会抛出一个异常 "InternalException: System error." 我 post 下面是我的代码,希望对您有所帮助,谢谢!
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.
谁能帮帮我?
我同意@stuartd 的评论。如果 "all works fine",并且您的文件 已 上传,但服务器实现不遵循 FTP 标准并且响应不正确或根本不响应,那么您唯一要做的就是可以做的是吞下超时异常,不要抛出,继续。
但是,与其捕获一般的 Exception
,不如尝试捕获最具体的异常类型(例如 WebException
),然后仍然捕获一般的异常类型,以便您可以记录并报告 其他 错误。