长期运行Web请求,处理来自服务器的数据,在线程中运行。 当网络电缆断开时,代码'oRe = bFmt.Deserialize(s);'无处可去。 我猜必须抛出异常,但无法捕获它。 如何捕获异常并处理它?</ p>
System.Net.HttpWebResponse response = null;
string url = "http://serverurl:port/Message/AsnyHandler.ashx?id=01020102011";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 1200000;
request.ReadWriteTimeout = 1200000;
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = false;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s = null;
object oRe = null;
try
{
while (true)
{
s = response.GetResponseStream();
if (s.CanRead)
{
BinaryFormatter bFmt = new BinaryFormatter();
oRe = bFmt.Deserialize(s);
if (oRe != null)
{
this.BeginInvoke(new DelegateProcessMsg(this.ProcessMsg), new object[1] { oRe });
oRe = null;
}
}
}
}
catch (IOException ex)
{
s.Close();
response.Close();
}
catch(Exception ex)
{
response.Close();
}