簡體   English   中英

使用HttpWebResponse時如何最好地處理異常

[英]How best to handle exceptions when using HttpWebResponse

我正在尋找有關如何處理以下代碼示例中拋出的任何異常的建議:

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;
}

我主要關心的是確保在方法結束時關閉StreamReader和HttpRequest對象。 我是不是該:

  1. 在try / catch / finally中記錄批次,在catch塊中記錄任何異常並關閉finally塊中的流?
  2. 在創建StreamReader時,在HttpWebRequest對象實例化和嵌套的using語句中使用using語句?
  3. 不用擔心它並假設當對象超出范圍時GC將清除所有內容,因為退出方法?

編輯 :進一步的調查顯示,選項2可以在不嵌套using語句的情況下完成:

    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不是一個好主意。

我個人非常喜歡Using statement route。 但是,try / catch / finally路由基本相同,並為您提供所需的日志記錄機制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM