。网。异步 HttpWebRequest 的最大数量

发布于 2024-12-20 16:27:59 字数 1470 浏览 0 评论 0原文

我需要在我的应用程序中处理并发请求。我使用此代码:

public class AsyncHttpHelper
{
public static IEnumerable<XDocument> GetPagesAsXDocuments(IEnumerable<string> uris)
{
    IEnumerable<IAsyncResult> asyncResults = uris
        .Select(uri => (HttpWebRequest)WebRequest.Create(uri))
        .Select(webRequest => webRequest.BeginGetResponse(null, webRequest));

    WaitHandle[] handles = asyncResults.Select(asyncResult => asyncResult.AsyncWaitHandle).ToArray();
    WaitHandle.WaitAll(handles);
    var result = asyncResults
        .Select(asyncResult =>
        {
            var httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
            }
            catch (Exception ex)
            {
                return null;
            }
            Stream responseStream = response.GetResponseStream();
            if (responseStream == null)
            {
                return null;
            }
            using (var streamReader = new StreamReader(responseStream))
            {
                return XDocument.Load(streamReader);
            }

        });
    return result;
}

}

但它仅适用于 1 个 url 请求。当我尝试获取 2 个或更多 url 时,我的方法挂起。当我暂停调试器时,我会显示执行 WaitHandle.WaitAll(handles); 这行代码。所以我看到并非所有异步请求都已完成。

那么有什么问题呢。为什么我不能异步执行多个请求

谢谢安德鲁

I need process concurent requests at my application. I use this code:

public class AsyncHttpHelper
{
public static IEnumerable<XDocument> GetPagesAsXDocuments(IEnumerable<string> uris)
{
    IEnumerable<IAsyncResult> asyncResults = uris
        .Select(uri => (HttpWebRequest)WebRequest.Create(uri))
        .Select(webRequest => webRequest.BeginGetResponse(null, webRequest));

    WaitHandle[] handles = asyncResults.Select(asyncResult => asyncResult.AsyncWaitHandle).ToArray();
    WaitHandle.WaitAll(handles);
    var result = asyncResults
        .Select(asyncResult =>
        {
            var httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
            }
            catch (Exception ex)
            {
                return null;
            }
            Stream responseStream = response.GetResponseStream();
            if (responseStream == null)
            {
                return null;
            }
            using (var streamReader = new StreamReader(responseStream))
            {
                return XDocument.Load(streamReader);
            }

        });
    return result;
}

}

But it correctly works only with 1 url for request. When I try get 2 or more urls my method hangs up. When I pause debugger shows me executing WaitHandle.WaitAll(handles); this line. So I see that not all async requests were done.

So what's the problem. Why I can't do several requests async

Thanks Andrew

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

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

发布评论

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

评论(2

小傻瓜 2024-12-27 16:27:59

这可能是服务点管理器的默认连接限制太低的问题。

(@mfeingold:这就是你的意思吗?我认为它默认为 2)
尝试将属性:设置为更高的值。

System.Net.ServicePointManager.DefaultConnectionLimit

在创建 HttpWebRequest 之前

It could be an issue of the service point manager's default connection limit being too low.

(@mfeingold: is this the setting you mean? I think it defaults to 2)
try setting the property:

System.Net.ServicePointManager.DefaultConnectionLimit

to something higher before creating the HttpWebRequest.

開玄 2024-12-27 16:27:59

我不记得了,但 web.config 中有一个设置指定同时传出的 Web 请求的数量。据我所知,默认的数字非常低,我相信它是 2

I do not remember off the top of my head, but there was a setting in the web.config specifying the number of simultaneous outgoing web requests. As far as I can remember the default for the number was really low I believe it was 2

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