Evitare errori quando si fa una WebRequest in VB.Net
-
13-10-2019 - |
Domanda
Ricevo tre errori utilizzando la seguente funzione:
Private Sub readWebpage(ByVal url As String)
Try
Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse = req.GetResponse
If Not resp.ContentType.Contains("text/html") And Not resp.ContentType.Contains("application/xhtml+xml") Then Exit Sub
Using Str As System.IO.Stream = resp.GetResponseStream
Using srRead As System.IO.StreamReader = New System.IO.StreamReader(Str)
parseHtml(srRead.ReadToEnd, url)
End Using
End Using
Catch ex As Exception
Debug.Print(ex.Message)
Debug.Print(url)
MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)
End Try
End Sub
Il server remoto ha restituito un errore: (500) Internal Server Error.
Il server ha commesso un protocollo violazione. Sezione = ResponseStatusLine
Il nome remoto non potrebbe essere risolto: 'Dmoz.orgsearch'
Come potrei essere in grado di evitare questi errori?
Soluzione
Non si può evitare errori remoti. Il meglio che puoi raggiungere è catturare le eccezioni il modo in cui si sono già.
Vorrei però suggerire che si sta utilizzando un URL non corretto -. dmoz.orgsearch
non esiste
È stato questo dovrebbe essere:? http://www.dmoz.org/search?q=myquery
Se l'URL che si sta utilizzando non è corretto, non c'è molto che si può fare al riguardo.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow