Oppure

Loading
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
06/12 20:20
Carlo
Modifica la domanda con [ edit ] e per il codice usa il tastino [ Code ], si trova a sinistra della finestra dove scrivi il messaggio.

Il codice verrà visualizzato così:
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 


Specifica gli Imports che hai usato e indica se hai inserito nei riferimenti qualche .dll, altrimenti così è ambiguo.

Dacci l'URL se non è privato, i siti non rispondono tutti allo stesso modo.

Ultima modifica effettuata da Carlo 06/12 20:37
in programmazione tutto è permesso