C#异步Http请求,如何停止跳过?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码的问题在于 BeginGetResponse(null, null) 接受回调作为操作完成时调用的第一个参数。此回调是应该调用 EndGetResponse 的地方。当您调用 EndGetResponse 时,操作尚未完成。
查看这篇文章,了解如何使用迭代器在 C# 中发出 aync Web 请求:http://tomasp .net/blog/csharp-async.aspx。
如果使用任务并行库或.NET 4,您也可以这样做:
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:
您尝试使用异步请求方法来执行同步请求,但这是行不通的。
您应该使用
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 callEndGetResponse
immediately afterBeginGetResponse
, 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.当我阅读 http://msdn.microsoft.com/ en-us/library/system.net.httpwebrequest.endgetresponse.aspx 您需要等待回调才能使用 EndGetResponse 吗?
或者使用 GetReponse: http://msdn.microsoft.com /en-us/library/system.net.httpwebrequest.getresponse.aspx
As I read http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.endgetresponse.aspx you need to wait for the callback before you can use EndGetResponse?
Or use GetReponse: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx