U
U
User Unknown2020-07-31 18:55:51
C++ / C#
User Unknown, 2020-07-31 18:55:51

How to ignore an error when the server response is different from 200?

A request is made to the server, which returns a 401 code, which is normal behavior. How to make it ignore that the code is different from 200? If you catch in try catch, then the answer is not returned, it is null.

var req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse res;
try {
   res = (HttpWebResponse)req.GetResponse();
   // Сервер возвращает код 401 
}
catch {
   // Здесь res = null 
}


5f243edb18430349384497.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
User Unknown, 2020-07-31
@aveBHS

I found the solution myself, it turns out that the error is transmitted and the server response

var req = WebRequest.Create(url);
WebResponse res;
try {
   res = req.GetResponse();
}
catch(WebException ex) {
   res = ex.Response;
}

I
Ivan Shumov, 2020-07-31
@inoise

what prevents to make throw?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question