06/12 18:12
Antonio4910
Quando eseguo il codice mi vengono segnalate in Output segnalazioni di errore (IOException e HTTPrequestException) che però non riesco ad intercettare. QUalcuno a qualche suggerimento? Grazie
Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of (String, String))
Dim maxRetries As Integer = 3 ' Numero massimo di tentativi
For attempt As Integer = 1 To maxRetries
Try
Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
Using content As HttpContent = response.Content
Dim urlContents As String = Await content.ReadAsStringAsync(ct)
Return (url, urlContents)
End Using
Catch ex As HttpRequestException
If attempt < maxRetries Then
Task.Delay(1000).Wait() ' Ritenta dopo un secondo di attesa
Else
Throw New Exception("Errore di I/O dopo i tentativi di ritentativo."
End If
Catch ioEx As IOException
If attempt < maxRetries Then
Task.Delay(1000).Wait() ' Ritenta dopo un secondo di attesa
Else
Throw New Exception("Errore di I/O dopo i tentativi di ritentativo."
End If
Catch ex As Exception
MessageBox.Show("Errore generale: " & ex.Message)
End Try
Next
End Function
Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of (String, String))
Dim maxRetries As Integer = 3 ' Numero massimo di tentativi
For attempt As Integer = 1 To maxRetries
Try
Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
Using content As HttpContent = response.Content
Dim urlContents As String = Await content.ReadAsStringAsync(ct)
Return (url, urlContents)
End Using
Catch ex As HttpRequestException
If attempt < maxRetries Then
Task.Delay(1000).Wait() ' Ritenta dopo un secondo di attesa
Else
Throw New Exception("Errore di I/O dopo i tentativi di ritentativo."
End If
Catch ioEx As IOException
If attempt < maxRetries Then
Task.Delay(1000).Wait() ' Ritenta dopo un secondo di attesa
Else
Throw New Exception("Errore di I/O dopo i tentativi di ritentativo."
End If
Catch ex As Exception
MessageBox.Show("Errore generale: " & ex.Message)
End Try
Next
End Function