1

a long run web request, processing data from server, run in a thread. when the network cable is disconnected, the code 'oRe = bFmt.Deserialize(s);' goes nowhere. i guess there must be an exception thrown,but cannot catch it. how to catch the exception,and handle it?

 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();
}
Roger.Wang
  • 11
  • 2
  • Wrap your `response` and `s` variables in `using` statements, remove the explicit exception swallowing, and see what the actual exceptions are. Especially because at the moment, you *only* clean up these objects in exceptional circumstances. In normal usage, they are left un-`Close`-d. – Damien_The_Unbeliever Jul 05 '13 at 06:38
  • What do you mean it "goes nowhere"? Damien has a good point on using-statements. Also, why do you loop? Calling `GetResponseStream()` should return you the same stream over and over again I think. So, does this code ever fail or does it just drain your resources until your application dies with OutOfMemoryException? Also, that code won't compile because s is declared in an inner scope and can't be accessed in the catch clause. – Anton Jul 05 '13 at 07:19
  • This code `response = (System.Net.HttpWebResponse)request.GetResponse();` is outside the try block, and likely the line where the exception gets thrown. So the exception is not caught by your catches. – user1429080 Jul 05 '13 at 08:02
  • Sorry, I ommit some code from the real code. the real code is too long. "goes nowhere" means the thread stop here, no any exeception or error message. – Roger.Wang Jul 05 '13 at 08:11
  • it's a long-run web request, the server hold a connection to the request and send data to the request at irregular intervals, so the client need a loop to receive data continuously. and the code runs ok, if the network is stable. – Roger.Wang Jul 05 '13 at 08:26
  • the key problem is : step the code to the line " oRe = bFmt.Deserialize(s);",and disconnect the network, and then go, and then the thread crack without any exception. – Roger.Wang Jul 05 '13 at 08:43
  • Try adding yet on more catch: `catch { response.Close() }` and see if the debugger steps into it. See [here](http://stackoverflow.com/a/10807022/1429080) for more about this. It's a long shot, but worth trying anyway. – user1429080 Jul 05 '13 at 09:55
  • Damien_The_Unbeliever:great! thanks so much! Solved the problem! – Roger.Wang Jul 10 '13 at 05:35

0 Answers0