【发布时间】:2014-12-24 00:48:54
【问题描述】:
我使用了这里的代码https://stackoverflow.com/a/16977731/2262863,当服务器返回 404 响应时遇到了问题。只有当响应返回 200 时,我才添加到处理 url。但据我所见,代码不会继续使用其他网址。这是我得到的一个错误:
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
试图停止任务,但遇到了同样的问题。
【问题讨论】:
-
您的
GetResponse或GetResponseStream正在抛出未处理的异常(在您有机会检查响应状态代码之前)。只要您小心,一个简单的try/catch就足够了。请注意,该链接指向一个问题,即专门要求 .NET 4.0 解决方案。如果您的目标是 4.5,Parallel.ForEach和任务延续并不真正合适,因为您可以(并且应该)在 IO 绑定的场景中使用async/await。 -
@Kirill Shlenskiy 我正在使用 NET 4.0 我试图捕捉,但无法添加 Continue For,这是我更改的
Using response = DirectCast(request.GetResponse(), HttpWebResponse) If response.StatusCode = System.Net.HttpStatusCode.OK Then,现在它运行 webresponse 阅读。 -
@KirillShlenskiy
try/catch方法有效,谢谢。
标签: vb.net task-parallel-library parallel.foreach