0

I have used code from here https://stackoverflow.com/a/16977731/2262863 and faced an issue when server returns 404 response. I have added to process url only if response have been 200 returned. But from what I see, code doesn`t continue with other urls. Here is an error I am getting:

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

Tried to stop task, but got same issue.

Community
  • 1
  • 1
Chelovek
  • 159
  • 2
  • 3
  • 12
  • Your `GetResponse` or `GetResponseStream` is throwing an exception which is not handled (before you have the chance to check the response status code). A simple `try/catch` will suffice as long as you are careful. Mind you, the link points to a question where a .NET 4.0 solution was specifically requested. If you're targeting 4.5, `Parallel.ForEach` and task continuations are not really appropriate as you can (and should) use `async/await` in scenarios where you are IO-bound. – Kirill Shlenskiy Dec 24 '14 at 01:06
  • @Kirill Shlenskiy I am using NET 4.0 I tried to catch, but there is no way to add Continue For, Here is what I changed `Using response = DirectCast(request.GetResponse(), HttpWebResponse) If response.StatusCode = System.Net.HttpStatusCode.OK Then` and now it run webresponse reading. – Chelovek Dec 24 '14 at 01:11
  • @KirillShlenskiy `try/catch` method works, thank you. – Chelovek Dec 24 '14 at 11:19

1 Answers1

0

After the line where Sub(word) starts I have added try/catch method.

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)

So the tasks are continued and process doesn`t stop.

Chelovek
  • 159
  • 2
  • 3
  • 12