【发布时间】:2011-04-03 22:41:09
【问题描述】:
这是一个使用 .NET 4.0 的 C# 应用程序。
背景:
我一直在尝试改进从多个 Web 服务器下载文件的应用程序的一部分。除了远程计算机关闭连接的偶尔情况外,一切都运行良好。我认为这可能是由于多种因素造成的,包括网络问题、电源问题等。如果连接关闭,我想简单地跳过下载。然而,目前应用程序会导致一个异常,该异常由AppDomain.CurrentDomain.UnhandledException 的处理程序拾取。堆栈跟踪如下:
堆栈跟踪:
Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd()
at ProjectName.ClassNetwork.DownloadFile(String _IP, Int32 _Port, String _File)
at ProjectName.FormMain.GetIFile(ClassSensor Sensor, String& RawHolder, String[] FileData)
at ProjectName.FormMain.GetLegacyData(ClassSensor Sensor)
at ProjectName.FormMain.threader_Download(Object SensorObject)
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
我最初有 WebException 和 Exception 的 try/catch,但他们没有捕捉到这个特定的错误。我添加了最里面的 try/catch 试图避免这个问题,但无济于事。我应该提到这个方法是由 UI 线程以外的线程调用的,这可能是 try/catch 块似乎不起作用的部分原因。在多线程错误捕获方面,请原谅我是新手!
问题:
- 如何改进此方法并正确处理中途连接失败的情况?
代码:
private static object[] DownloadFile(string _IP, int _Port, string _File)
{
string uri = String.Format("http://{0}:{1}/{2}", _IP, _Port, _File);
string Status = String.Empty;
string Data = String.Empty;
HttpWebResponse Response = null;
try
{
HttpWebRequest webReq = (HttpWebRequest) WebRequest.Create(uri);
webReq.Timeout = 10000;
webReq.ReadWriteTimeout = 30000;
Response = (HttpWebResponse) webReq.GetResponse();
using (Stream dataStream = Response.GetResponseStream())
if (dataStream != null)
using (StreamReader reader = new StreamReader(dataStream))
try
{
// This line causes crashes if remote computer closes connection
Data = reader.ReadToEnd();
}
catch { } // Inner try/catch added to no avail
Response.Close();
}
catch (WebException exc)
{
// Connection Error
Status = exc.Status.ToString();
Data = String.Empty;
}
catch (Exception exc)
{
// Other error
Status = exc.Message;
Data = String.Empty;
}
if (Response != null && Response.StatusCode != HttpStatusCode.OK)
{
Status = Response.StatusCode.ToString();
Data = String.Empty;
}
return new object[] { Status, Data };
}
跟踪日志:
根据 Feroze 的建议,我运行了一个跟踪点,结果如下:
System.Net Error: 0 : [2164] Exception in the
#46104728::UnhandledExceptionHandler - Stream was not readable.
System.Net Error: 0 : [2164] at System.IO.BinaryReader..ctor(Stream input, Encoding encoding)
at ProjectName.ClassNetwork.DLStream(ClassSensor Sensor)
at ProjectName.ClassNetwork.DownloadFile(ClassSensor Sensor)
at ProjectName.FormMain.GetStreamFile(ClassSensor Sensor)
at ProjectName.FormMain.threader_Download(Object SensorObject)
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncctx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
【问题讨论】:
-
我找不到任何问号,表明您也想回答一个问题。
-
The Code 之前的最后一句话。如果您更愿意将其表述为一个问题:“我如何改进此方法并正确处理中途连接失败的情况?”
-
你能发布完整的(未修剪的)堆栈跟踪吗?
-
我将编辑问题以包含所有内容。
标签: c# exception-handling httpwebrequest