Csharp WinForm Net 4.5

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

The remote server returned an error: (404) Not found.

When parsing pages, sometimes there are pages that do not return an answer, as if the Remote server returned an error: (404) Not found. You need to do something like this

 if (response == null) { сохранить страницу в блек лист и начать процес заново } 

This is my presentation, but tell me, please, how correctly, I for some reason cannot do this inside the class?

    1 answer 1

    Wrap in try ... catch :

     try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { HttpWebResponse errorResponse = (HttpWebResponse)ex.Response; if (errorResponse.StatusCode == HttpStatusCode.NotFound) { // Обрабатываем 404 } }