C#异步Http请求,如何停止跳过?

发布于 2024-10-25 07:09:29 字数 870 浏览 2 评论 0原文

List<string> urls = this.populateRequestList();

this.Logger("Starting");

var reqs = urls.Select<string, WebRequest>(HttpWebRequest.Create).ToArray();
var iars = reqs.Select(req => req.BeginGetResponse(null, null)).ToArray();
var rsps = reqs.Select((req, i) => req.EndGetResponse(iars[i])).ToArray();

this.Logger("Done"); 

到目前为止我注意到的事情:
当我运行此代码时,“开始”显示在我的日志中,但“完成”从未显示。当我在调试器中查看整个过程时,它似乎跳过了它,就像它根本不存在一样。也没有抛出任何异常。当 reqs.Select 循环通过 req.EndGetResponse(iars[i]) 时,它就像冻结或跳过一些内容。当我在调试器中查看它时,我没有经过 10-15 个循环,它就跳到了末尾。

问题:
如何在 var rsps = reqs.Select((req, i) => req.EndGetResponse(iars[i])).ToArray(); 期间停止“跳过”此操作?

如何从rsps获取html?我认为这样做的问题源于“跳过”。我尝试循环遍历每个响应并调用 Repsponse.GetResponseStream() 等...,但是一旦跳过就什么也没有发生。

List<string> urls = this.populateRequestList();

this.Logger("Starting");

var reqs = urls.Select<string, WebRequest>(HttpWebRequest.Create).ToArray();
var iars = reqs.Select(req => req.BeginGetResponse(null, null)).ToArray();
var rsps = reqs.Select((req, i) => req.EndGetResponse(iars[i])).ToArray();

this.Logger("Done"); 

Things I noticed so far:
When I run this code, "Starting" shows up in my log, but "Done" never shows up. When I view the whole process in the debugger, it seems to skip over it like it's not even there. No exceptions are being thrown either. When reqs.Select is looping through req.EndGetResponse(iars[i]), it's like it freezes or skips over stuff. When I view it in the debugger, I don't get past 10-15 loops before it just skips to the end.

Questions:
How do I stop this from "skipping" sometime during var rsps = reqs.Select((req, i) => req.EndGetResponse(iars[i])).ToArray();?

How to I get the html from rsps? I think this problem doing that stems from the "skipping". I tried looping through each response and calling Repsponse.GetResponseStream() etc..., but nothing happens as soon as it skips.

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

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

发布评论

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

评论(3

明天过后 2024-11-01 07:09:29

您的代码的问题在于 BeginGetResponse(null, null) 接受回调作为操作完成时调用的第一个参数。此回调是应该调用 EndGetResponse 的地方。当您调用 EndGetResponse 时,操作尚未完成。

查看这篇文章,了解如何使用迭代器在 C# 中发出 aync Web 请求:http://tomasp .net/blog/csharp-async.aspx

如果使用任务并行库或.NET 4,您也可以这样做:

var urls = new List<string>();

            var tasks = urls.Select(url =>
                {
                    var request = WebRequest.Create(url);
                    var task = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                    task.Start();
                    return task;
                }).ToArray();

            Task.WaitAll(tasks);

            foreach (var task in tasks)
            {
                using (var response = task.Result)
                using (var stream = response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    var html = reader.ReadToEnd();
                }                                
            }

The problem with your code is that BeginGetResponse(null, null) accepts a callback as the first argument which is invoked when the operation completes. This callback is where EndGetResponse should be called. When you call EndGetResponse, the operations are not yet completed.

Look at this article to see how aync web requests can be made in C# using iterators: http://tomasp.net/blog/csharp-async.aspx.

If using the task parallel library or .NET 4 you can also do this:

var urls = new List<string>();

            var tasks = urls.Select(url =>
                {
                    var request = WebRequest.Create(url);
                    var task = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                    task.Start();
                    return task;
                }).ToArray();

            Task.WaitAll(tasks);

            foreach (var task in tasks)
            {
                using (var response = task.Result)
                using (var stream = response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    var html = reader.ReadToEnd();
                }                                
            }
看春风乍起 2024-11-01 07:09:29

您尝试使用异步请求方法来执行同步请求,但这是行不通的。

您应该使用 BeginGetResponse 以及处理每个响应的回调方法来启动请求。如果您在 BeginGetResponse 之后立即调用 EndGetResponse,则会失败,因为响应尚未开始到达。

如果您想发出同步请求,请改用 GetResponse 方法。

You are trying to use the asynchronous request methods to do a synchronous request, that doesn't work.

You are supposed to start the requests using BeginGetResponse with a callback method that handles each response. If you call EndGetResponse immediately after BeginGetResponse, it will fail because the response haven't started to arrive yet.

If you want to make a synchronous request, use the GetResponse method instead.

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