HttpWebRequest和IDisposable [英] HttpWebRequest and IDisposable

查看:55
本文介绍了HttpWebRequest和IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




以下代码有效:


HttpWebRequest objRequest = null;

try

{

HttpWebRequest objRequest =

(HttpWebRequest)WebRequest.Create(" http://www.microsoft.com");

使用(HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse())

{

//做点什么.. 。

}

}

catch

{

throw;

}

终于

{

if(objWebRequest!= null)

{

objWebRequest = null;

}

}


但是,以下代码不起作用:


使用(HttpWebRequest objRequest =

(HttpWebRequest)WebRequest.Create(" http://www.microsoft.com"))

{

使用(HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse())

{

//做点什么......

}

}


因为HttpWebRequest不能隐式转换为IDisposable。


我很想知道是否有一种方法可以使用HttpWebRequest和

" using" - 只是为了我自己的兴趣,真的......


感激不尽的任何帮助。


Mark

Hi,

The following code works:

HttpWebRequest objRequest = null;
try
{
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
using (HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse())
{
// do something...
}
}
catch
{
throw;
}
finally
{
if (objWebRequest != null)
{
objWebRequest = null;
}
}

However, the following code doesn''t work:

using (HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com"))
{
using (HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse())
{
// do something...
}
}

because HttpWebRequest is not implicitly convertible to IDisposable.

I''m curious to know if there is a way to use HttpWebRequest with the
"using" - just for my own interest, really...

Any assistance gratefully received.

Mark

推荐答案

(以下涉及所有一般变量用法;不是专门用于

web-request)


嗯,您认为您的代码有什么作用?


抓住{

throw;

}


什么都不做,因为它本来会被抛出......所以最后离开了




终于

{

if(objWebRequest!= null)

{

objWebRequest = null;

}

}


有趣的是,因为你读取了变量(在测试中),所以从技术上来说,* b $ b延长了变量的生命周期。否则它将有资格收集早期的b
。我猜这一切都取决于是否
objWebRequest是一个字段(由类作为范围)或变量(范围由

方法)。如果一个字段,罚款:只需删除捕获;如果一个变量,

那么就让它超出范围;一旦你上次阅读,它实际上可以用于GC

。在垃圾收集的世界中,使用

和最后。模式主要是为了及时处置资源。上面的

看起来更像是COM / VB6,但你不需要使用C#

变量。


Marc

(Following relates to all general variable usage; not specifically to
web-request)

Well, what do you think your code does?

catch {
throw;
}

Does nothing, as it would have been thrown anyway... so that leaves the
finally:

finally
{
if (objWebRequest != null)
{
objWebRequest = null;
}
}

Interestingly, because you read the variable (in the test), this
technically *extends* the lifetime of the variable. Otherwise it would
be eligible for collection earlier. I guess it all depends on whether
objWebRequest is a field (scoped by the class) or a variable (scoped by
the method). If a field, fine: just remove the catch; if a variable,
then just let it go out of scope; it will actually be avaiable for GC
as soon as your last "read". In a garbage collected world, the "using"
and "finally" patterns are mainly for timely disposal of resources. The
above looks more like COM / VB6, but you don''t need this with C#
variables.

Marc


嗨Mark,


< snip>
Hi Mark,

<snip>

但是,以下代码不起作用:


使用(HttpWebRequest objRequest =

(HttpWebRequest)WebRequest.Create(" http) ://www.microsoft.com"))

{

使用(HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse())

{

//做点什么......

}

}


,因为HttpWebRequest不能隐式转换为IDisposable。


我很想知道是否有办法使用HttpWebRequest和

"使用" - 仅仅为了我自己的利益,真的...
However, the following code doesn''t work:

using (HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com"))
{
using (HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse())
{
// do something...
}
}

because HttpWebRequest is not implicitly convertible to IDisposable.

I''m curious to know if there is a way to use HttpWebRequest with the
"using" - just for my own interest, really...



我认为WebRequest没有实现IDisposable很奇怪,因为它

可以创建一个Stream,它实现了IDisposable。不幸的是,

是你无法使用使用的这个类的声明,虽然你无论如何都不应该担心。


相反,你可能想要使用WebClient,它是IDisposable:


使用(WebClient客户端=新WebClient())

{

使用(Stream stream = client.OpenRead(url) )

{

//等

}

}


-

Dave Sexton

I think it''s strange that WebRequest doesn''t implement IDisposable since it
can create a Stream, which does implement IDisposable. Unfortunately, there
is no way for you to use the "using" statement with this class, although you
probably shouldn''t worry about that anyway.

Instead, you may want to use WebClient, which is IDisposable:

using (WebClient client = new WebClient())
{
using (Stream stream = client.OpenRead(url))
{
// etc.
}
}

--
Dave Sexton


" Marc Gravell" < ma ********** @ gmail.comwrote in message

news:11 ******************** **@n67g2000cwd.googlegr oups.com ...
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...

我猜这一切都取决于objWebRequest是否是一个字段

(范围由类)或变量(由方法确定范围)。
I guess it all depends on whether objWebRequest is a field
(scoped by the class) or a variable (scoped by the method).



它根本没有任何关系 - 你真的*读过*我的

原帖......? />

Dave Sexton理解并做出相应的回复 - 你可能希望在他的......

It has nothing whatsoever to do with that at all - did you actually*read* my
original post...?

Dave Sexton understood it and replied accordingly - you might want to have a
read at his...

上读取


这篇关于HttpWebRequest和IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆