Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HttpWebRequest exception handling

HttpWebRequest exception handling

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evgeni57
    wrote on last edited by
    #1

    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

    OriginalGriffO 1 Reply Last reply
    0
    • E Evgeni57

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups