How to better handle exceptions when using HttpWebResponse

I am looking for tips on how to handle exceptions in the following code example:

private string SendRequest()
{
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myURL);
        // Code initialising HttpWebRequest
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream rcvdStream = response.GetResponseStream();
        StreamReader readStream = new StreamReader(rcvdStream, Encoding.UTF8);
        string responseString = readStream.ReadToEnd();
        response.Close();
        readStream.Close();
        return responseString;
}

My main problem is to ensure that the StreamReader and HttpRequest objects close when the method ends. Should I:

  • Wrap the lot in try / catch /, finally, write down all the exceptions in the catch block and close the stream in the finally block.
  • Use a using statement to create an object of an HttpWebRequest object and a nested using statement when creating a StreamReader?
  • Don't worry about this and assume that the GC will clear everything when objects go out of scope when they exit the method?

EDIT . Further research showed that option 2 can be performed without nested statements:

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (Stream rcvdStream = response.GetResponseStream())
    {
        StreamReader readStream = new StreamReader(rcvdStream, Encoding.UTF8);
        payResponse = readStream.ReadToEnd();
    }

IL, , try/finally:

  IL_00b0:  callvirt   instance class [System]System.Net.WebResponse [System]System.Net.WebRequest::GetResponse()
  IL_00b5:  castclass  [System]System.Net.HttpWebResponse
  IL_00ba:  stloc.s    response
  .try
  {
    IL_00bc:  ldloc.s    response
    IL_00be:  callvirt   instance class [mscorlib]System.IO.Stream [System]System.Net.WebResponse::GetResponseStream()
    IL_00c3:  stloc.s    rcvdStream
    .try
    {
      IL_00c5:  nop
      IL_00c6:  ldloc.s    rcvdStream
      IL_00c8:  call       class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::get_UTF8()
      IL_00cd:  newobj     instance void [mscorlib]System.IO.StreamReader::.ctor(class [mscorlib]System.IO.Stream,
                                                                                 class [mscorlib]System.Text.Encoding)
      IL_00d2:  stloc.s    readStream
      IL_00d4:  ldloc.s    readStream
      IL_00d6:  callvirt   instance string [mscorlib]System.IO.TextReader::ReadToEnd()
      IL_00db:  stloc.3
      IL_00dc:  nop
      IL_00dd:  leave.s    IL_00f3
    }  // end .try
    finally
    {
      IL_00df:  ldloc.s    rcvdStream
      IL_00e1:  ldnull
      IL_00e2:  ceq
      IL_00e4:  stloc.s    CS$4$0001
      IL_00e6:  ldloc.s    CS$4$0001
      IL_00e8:  brtrue.s   IL_00f2
      IL_00ea:  ldloc.s    rcvdStream
      IL_00ec:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
      IL_00f1:  nop
      IL_00f2:  endfinally
    }  // end handler
    IL_00f3:  nop
    IL_00f4:  leave.s    IL_010a
  }  // end .try
  finally
  {
    IL_00f6:  ldloc.s    response
    IL_00f8:  ldnull
    IL_00f9:  ceq
    IL_00fb:  stloc.s    CS$4$0001
    IL_00fd:  ldloc.s    CS$4$0001
    IL_00ff:  brtrue.s   IL_0109
    IL_0101:  ldloc.s    response
    IL_0103:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
    IL_0108:  nop
    IL_0109:  endfinally
  }  // end handler
+3
1

1 2 - . 3 .

Statement. , try/catch/finally , .

+2

Source: https://habr.com/ru/post/1705968/


All Articles