【问题标题】:404 httpwebrequest exception in parallel tasks并行任务中的 404 httpwebrequest 异常
【发布时间】: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

试图停止任务,但遇到了同样的问题。

【问题讨论】:

  • 您的GetResponseGetResponseStream 正在抛出未处理的异常(在您有机会检查响应状态代码之前)。只要您小心,一个简单的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


【解决方案1】:

Sub(word) 开始的那一行之后,我添加了try/catch 方法。

Try Dim url = word Dim request = DirectCast(WebRequest.Create(url), HttpWebRequest)

.......

End Using Catch ex As Exception Console.WriteLine(ex.toString) End Try End Sub)

所以任务会继续,过程不会停止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多