WebClient: ignore HTTP 500

I am writing a program that retrieves some data from a server, performs some operations on it, and saves the output to a csv file. The problem is that the server (for which I am not responsible) ALWAYS returns an internal error of the HTTP 500 server. I talked to the team that monitors it, and although they know about the error, they said that this does not affect their decision .

Is there a way to ignore this answer in my code and still receive data?

+4
source share
1 answer

If you are using HttpWebRequest / Response, you should start:

response = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<url>"); response = (HttpWebResponse)request.GetResponse(); //no error } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { response = (HttpWebResponse)e.Response; if((int)response.StatusCode == 500) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { var result = sr.ReadToEnd(); } } } } 
+6
source

Source: https://habr.com/ru/post/1445437/


All Articles