C# 何时调用 WebResponse.Close()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1184422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to call WebResponse.Close()
提问by Sameet
WebResponse response;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
response = request.GetResponse();
request = (HttpWebRequest)WebRequest.Create(url2);
response = request.GetResponse();
}
catch(Exception ex)
{
//do something
}
finally
{
}
where should response.Close() be called?
应该在哪里调用 response.Close() ?
after every GetResponse() in try?
after last GetResponse() in try - once?
- in finally block?
在尝试每个 GetResponse() 之后?
在尝试中的最后一次 GetResponse() 之后 - 一次?
- 在最后块?
采纳答案by John Saunders
None of the above. You should be using a using
block:
以上都不是。你应该使用一个using
块:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
using (WebResponse response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
// Do something with result
}
}
}
A using
block will ensure that the Dispose method is called, whether or not there is an exception. Dispose will do the same thing as Close.
阿using
块将确保Dispose方法被调用时,无论是否有异常。Dispose 将执行与 Close 相同的操作。
using (var d = new DisposableClass()){code;}
is equivalent to:
相当于:
DisposableClass d = null;
try
{
d = new DisposableClass();
code;
}
finally
{
if (d != null)
((IDisposable)d).Dispose();
}
回答by marco0009
Put it in the finally block. As per MSDN:
把它放在 finally 块中。根据MSDN:
The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.
finally 块对于清理在 try 块中分配的任何资源以及运行任何即使出现异常也必须执行的代码非常有用。不管 try 块如何退出,控制总是传递给 finally 块。
回答by Ramesh
I would suggest the below
我会建议以下
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Timeout = 20000;
using (var response = request.GetResponse())
{
//Do something with response.
}
request = (HttpWebRequest)WebRequest.Create("http://www.bing.com");
using (var response = request.GetResponse())
{
//Do somehing with response
}
}
catch (Exception ex)
{
//do something
}
finally
{
}
回答by Eamon Nerbonne
Note that nested using blocks don't need curly braces, improving readability. So John Saunder's code could be written:
请注意,嵌套 using 块不需要花括号,提高了可读性。所以约翰桑德的代码可以写成:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
using (WebResponse response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
// Do something with result
}
VS.NET understands that such nested blocks don't need indenting. Note btw that if you know the encoding of the response or are going to ignore it anyhow, WebClient provides a simpler API - missing header information, so Header-based (transfer/text) encoding detection becomes impossible, but otherwise it works fine.
VS.NET 理解这样的嵌套块不需要缩进。请注意,如果您知道响应的编码或无论如何要忽略它,WebClient 提供了一个更简单的 API - 缺少标头信息,因此基于标头(传输/文本)的编码检测变得不可能,但否则它可以正常工作。