。网。异步 HttpWebRequest 的最大数量
我需要在我的应用程序中处理并发请求。我使用此代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是服务点管理器的默认连接限制太低的问题。
(@mfeingold:这就是你的意思吗?我认为它默认为 2)
尝试将属性:设置为更高的值。
在创建 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:
to something higher before creating the
HttpWebRequest
.我不记得了,但 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