HttpWebRequest exception handling
-
I have some function, which makes HttpWebRequest and returns response (string). Everything works fine except cases when it gets 502 status - Bad Gateway. In this case try-catch block:
try
{
m_Rresponse = (HttpWebResponse)m_HttpWebRequest.GetResponse();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (m_Rresponse.StatusCode == HttpStatusCode.BadGateway || m_Rresponse.StatusCode == HttpStatusCode.GatewayTimeout ||
m_Rresponse.StatusCode == HttpStatusCode.InternalServerError || m_Rresponse.StatusCode == HttpStatusCode.ServiceUnavailable)
{
Console.WriteLine("Sleeping 10 seconds...");
Thread.Sleep(10000);
}
}The question is why In case of BadGatway "Sleeping 10 seconds..." never printed? Thanks
-
I have some function, which makes HttpWebRequest and returns response (string). Everything works fine except cases when it gets 502 status - Bad Gateway. In this case try-catch block:
try
{
m_Rresponse = (HttpWebResponse)m_HttpWebRequest.GetResponse();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (m_Rresponse.StatusCode == HttpStatusCode.BadGateway || m_Rresponse.StatusCode == HttpStatusCode.GatewayTimeout ||
m_Rresponse.StatusCode == HttpStatusCode.InternalServerError || m_Rresponse.StatusCode == HttpStatusCode.ServiceUnavailable)
{
Console.WriteLine("Sleeping 10 seconds...");
Thread.Sleep(10000);
}
}The question is why In case of BadGatway "Sleeping 10 seconds..." never printed? Thanks
Since I don't have any way of generating a 502 error, I have to assume that is does not set m_Rresponse when it throws an exception - which makes sense, since the
throw
instruction will terminate processing and transfer control to the catch block immediately. The documentation[^] says "If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server." which makes more sense to me than relying on an exception completing the instruction that detected the problem. I am surprised it works for any error, not just 502!You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Since I don't have any way of generating a 502 error, I have to assume that is does not set m_Rresponse when it throws an exception - which makes sense, since the
throw
instruction will terminate processing and transfer control to the catch block immediately. The documentation[^] says "If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server." which makes more sense to me than relying on an exception completing the instruction that detected the problem. I am surprised it works for any error, not just 502!You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
OriginalGriff wrote:
I am surprised it works for any error
That makes a lot of sense. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.