取消异步 httpweb 请求

发布于 2024-10-29 11:32:29 字数 1036 浏览 6 评论 0原文

我正在制作一个应用程序,该应用程序将制作多个 HttpWebRequest 对象并通过 httpRequest.BeginGetResponse 方法下载我的 html。我取回 IAsyncResult 并将其存储在本地,以便我可以随时取消请求,但我不确定我是否正确执行了该操作。

以下是我取消异步 Web 请求的操作:

var res = (RequestState)asyncResult.AsyncState;
res.Request.Abort();

其中 Request 的类型为 HttpWebRequest

我注意到的是,即使在调用这些代码行之后,我的应用程序中仍然打开了所有异步线程。如果我在 httpRequest.BeginGetResponse(GetResponseCallback, state) 中调用的委托中设置断点(例如方法 GetResponseCallback),调试器会在几秒钟后在方法内部中断,导致运行该方法时抛出 WebException。

为了完整起见,我的 GetResponseCallback 如下所示:

using (var httpWebResponse = (HttpWebResponse)request.EndGetResponse(result))
using (Stream dataStream = httpWebResponse.GetResponseStream())
using (var reader = new StreamReader(dataStream))
{
    string ret = reader.ReadToEnd();
    state.OnComplete(ret, new EventArgs());
}

我在 using (Stream dataStream = httpWebResponse.GetResponseStream()) 行上收到 WebException。内部异常表示诸如“服务器主动拒绝连接”之类的内容。

任何帮助都会非常好!

I am making an app that will make several HttpWebRequest objects and downloading my html via the httpRequest.BeginGetResponse method. I get back the IAsyncResult and store it locally so that I can cancel the request at any time, but I'm not sure if I'm doing that correctly.

Here is what I'm doing to cancel the async web request:

var res = (RequestState)asyncResult.AsyncState;
res.Request.Abort();

Where Request is of type HttpWebRequest.

What I'm noticing is that even after I call these lines of code, I still have all of the Async threads open in my application. And if I set a break point in the delegate called in httpRequest.BeginGetResponse(GetResponseCallback, state) (e.g. the method GetResponseCallback) The debugger breaks inside the method after a few seconds, causing a WebException to be thrown when that method is ran.

Just for completeness, my GetResponseCallback looks like this:

using (var httpWebResponse = (HttpWebResponse)request.EndGetResponse(result))
using (Stream dataStream = httpWebResponse.GetResponseStream())
using (var reader = new StreamReader(dataStream))
{
    string ret = reader.ReadToEnd();
    state.OnComplete(ret, new EventArgs());
}

and I get the WebException on the using (Stream dataStream = httpWebResponse.GetResponseStream()) line. The inner exception says something like "the server actively refused the connection" or something like that.

Any help would be excellent!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-11-05 11:32:29

来自HttpWebRequest.Abort() 的文档

Abort 方法取消对资源的请求。取消请求后,调用 GetResponse、BeginGetResponse、EndGetResponse、GetRequestStream、BeginGetRequestStream 或 EndGetRequestStream 方法会导致出现 WebException,且 Status 属性设置为 RequestCanceled。

所以你所描述的行为是设计使然的。我认为您必须捕获异常,或者找到某种方法来确定在调用 GetResponse、BeginGetResponse、EndGetResponse、GetRequestStream、BeginGetRequestStream 或 EndGetRequestStream 之前是否调用了 Abort。

From the documentation for HttpWebRequest.Abort():

The Abort method cancels a request to a resource. After a request is canceled, calling the GetResponse, BeginGetResponse, EndGetResponse, GetRequestStream, BeginGetRequestStream, or EndGetRequestStream method causes a WebException with the Status property set to RequestCanceled.

So the behavior you describe is by design. I think you'll have to catch the exception, or else find some way of determining whether Abort was called before calling GetResponse, BeginGetResponse, EndGetResponse, GetRequestStream, BeginGetRequestStream, or EndGetRequestStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文