2017-10-09 104 views
1

考虑从微软DOC采取了以下功能:正确处置HttpWebResponse异常

void webREquest(url) { 
    // taken from https://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx 
    try { 
    // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name. 
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url); 

    // Get the associated response for the above request. 
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); 
    myHttpWebResponse.Close(); 
    } 
    catch(WebException e) { 
    Console.WriteLine("This program is expected to throw WebException on successful run."+ 
       "\n\nException Message :" + e.Message); 
    if(e.Status == WebExceptionStatus.ProtocolError) { 
     Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode); 
     Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription); 
    } 
    } 
    catch(Exception e) { 
    Console.WriteLine(e.Message); 
    } 
} 

我有对象的方式有几个问题设置或不适:

  • myHttpWebRequest不直接关闭/处置 - 这是真的不必要?在这种情况下请求何时关闭?我怎样才能确保它尽快关闭?

  • WebException catch块中,是否不需要以某种方式处置/关闭e.Response(其类型为HttpWebResponse)?如果不是在哪里/如何处置?

  • myHttpWebRequestmyHttpWebResponse附近有using子句不是更好吗?但如果是这样,这将使异常处理程序中的e.Response无法访问?

  • 上面的“这个程序预计会在成功运行时抛出WebException”是什么意思?

我需要确保没有任何关系停留在函数退出后...

+0

[请求'.Content'设置在所述请求被发送之后。](即请求被到无效的网站作出提到https://stackoverflow.com/questions/30643994/given-a-httpresponsemessage-how-do-iread-the-content-of-the-request/30881737#30881737) –

+0

我不明白这是什么关系? – kofifus

回答

1

myHttpWebRequest不能直接关闭/配置 - 这是真的 不必要的?在这种情况下请求何时关闭?我怎样才能让 确定它尽快关闭?

是的,但考虑到类没有实现一个IDisposable接口,它可能看起来没问题。

在引发WebException catch块

,是它没有必要以某种方式处置 /关闭e.Response(这是类型HttpWebResponse的)?如果不是 在哪里/如何处理?

我会建议在最后关闭它阻止同时处理异常和非厚望场景

岂不是最好有一个使用条款各地myHttpWebRequest 和myHttpWebResponse?但如果是这样,那么这会使得 异常处理程序中的e.Response不可访问吗?

不幸的HttpWebRequest没有实现IDisposable所以你不能使用在这种情况下

是什么意思“这个程序应该抛出 引发WebException上成功运行”上面使用?

它在注释因此预期抛出异常

void webREquest(string url) 
     { 
      // taken from https://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx 
      HttpWebRequest myHttpWebRequest = null; 
      HttpWebResponse myHttpWebResponse = null; 
      try 
      { 
       // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name. 
       myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 

       // Get the associated response for the above request. 
       myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
       myHttpWebResponse.Close(); 
      } 
      catch (WebException e) 
      { 
       Console.WriteLine("This program is expected to throw WebException on successful run." + 
          "\n\nException Message :" + e.Message); 
       if (e.Status == WebExceptionStatus.ProtocolError) 
       { 
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode); 
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
      } 
      finally 
      { 
       //close on myHttpWebResponse 
       myHttpWebResponse?.Close(); 

       //mark myHttpWebRequest for collection 
       myHttpWebRequest = null; 
      } 
     } 
+0

thx! ...“我会建议在finally块中关闭它以处理异常和非预期场景” - 你能显示示例代码吗? – kofifus