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. XML from URL Causes Internal Server Error (500)

XML from URL Causes Internal Server Error (500)

Scheduled Pinned Locked Moved C#
helpxmlcsharpdatabasesysadmin
6 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
    Enochs
    wrote on last edited by
    #1

    I use a web based look-up service to obtain detailed information on IP addresses. It works great until the service goes down and the URL fails to render proper XML. The error (when the service is down) occurs on the "xDoc = XDocument.Load(url)" statement but does NOT throw an XmlException that I can catch...instead the server (I use a host) locks up with an internal (500) error. Since this error is from the server and not my application, I get no stack trace. All of my XML validation methods are predicated on the fact that I have the XDocument loaded first....then validate against an XML schema. But it's the load that fails so I have no chance to validate the XML. Any Ideas?

    string url = String.Format("http://www.ipgp.net/api/xml/{0}/{1}", sourceIP, password);
    try
    { /* This returns an internal server
    * error (500) if the url fails to
    * render proper xml. No error
    * gets thrown. */
    xDoc = XDocument.Load(url);
    }
    catch (XmlException ex)
    {
    /* This is a safe xml document
    * that I would like to load if
    * the load fails. */
    xDoc = XDocument.Load(safe);
    }

    UPDATE: I figured out this UGLY hack to get around the problem for now. The following code first reads the webpage into a variable and then checks the variable for an XML tag that I know should be present. If XML tag can't be found then my default (safe) xml loads. I hope someone can PLEASE give me a better fix...thanks

    HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "GET";
    WebResponse myResponse = myRequest.GetResponse();
    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    string result = sr.ReadToEnd();
    sr.Close();
    myResponse.Close();

    if (ExactMatch(result, ""))
    xDoc = XDocument.Load(url);
    else
    xDoc = XDocument.Load(Safe);

    F J 2 Replies Last reply
    0
    • E Enochs

      I use a web based look-up service to obtain detailed information on IP addresses. It works great until the service goes down and the URL fails to render proper XML. The error (when the service is down) occurs on the "xDoc = XDocument.Load(url)" statement but does NOT throw an XmlException that I can catch...instead the server (I use a host) locks up with an internal (500) error. Since this error is from the server and not my application, I get no stack trace. All of my XML validation methods are predicated on the fact that I have the XDocument loaded first....then validate against an XML schema. But it's the load that fails so I have no chance to validate the XML. Any Ideas?

      string url = String.Format("http://www.ipgp.net/api/xml/{0}/{1}", sourceIP, password);
      try
      { /* This returns an internal server
      * error (500) if the url fails to
      * render proper xml. No error
      * gets thrown. */
      xDoc = XDocument.Load(url);
      }
      catch (XmlException ex)
      {
      /* This is a safe xml document
      * that I would like to load if
      * the load fails. */
      xDoc = XDocument.Load(safe);
      }

      UPDATE: I figured out this UGLY hack to get around the problem for now. The following code first reads the webpage into a variable and then checks the variable for an XML tag that I know should be present. If XML tag can't be found then my default (safe) xml loads. I hope someone can PLEASE give me a better fix...thanks

      HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(url);
      myRequest.Method = "GET";
      WebResponse myResponse = myRequest.GetResponse();
      StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
      string result = sr.ReadToEnd();
      sr.Close();
      myResponse.Close();

      if (ExactMatch(result, ""))
      xDoc = XDocument.Load(url);
      else
      xDoc = XDocument.Load(Safe);

      F Offline
      F Offline
      fjdiewornncalwe
      wrote on last edited by
      #2

      To see what is going on, you could just load the document from the url into a string first so that you can see if it is an issue with the request or response first. Once you have the string you just need to parse it into your XmlDocument. It adds a line of code, but it makes debugging and viewing your variables much easier.

      I wasn't, now I am, then I won't be anymore.

      E 1 Reply Last reply
      0
      • F fjdiewornncalwe

        To see what is going on, you could just load the document from the url into a string first so that you can see if it is an issue with the request or response first. Once you have the string you just need to parse it into your XmlDocument. It adds a line of code, but it makes debugging and viewing your variables much easier.

        I wasn't, now I am, then I won't be anymore.

        E Offline
        E Offline
        Enochs
        wrote on last edited by
        #3

        That is what my "Ugly Hack" that I posted does. Using 7 lines of code, it reads the XML into a string and looks at that string before deciding if the XML is to be loaded. Just seems like a terrible waste of time/resources. I don't understand why it doesn't simple kick out an XmlException as the documentation says it should. The server 500 error gets me every time. Thanks for responding.

        F 1 Reply Last reply
        0
        • E Enochs

          That is what my "Ugly Hack" that I posted does. Using 7 lines of code, it reads the XML into a string and looks at that string before deciding if the XML is to be loaded. Just seems like a terrible waste of time/resources. I don't understand why it doesn't simple kick out an XmlException as the documentation says it should. The server 500 error gets me every time. Thanks for responding.

          F Offline
          F Offline
          fjdiewornncalwe
          wrote on last edited by
          #4

          My hunch is that the error isn't coming from your own code. Are you running this code in a web application? If so, then maybe, but if your application isn't a web application then the likely source of the 500 error is the site you are trying to hit. If the source site is working properly when you enter your full url into a browser, then you may have an issue with how your url is constructed as well.

          I wasn't, now I am, then I won't be anymore.

          E 1 Reply Last reply
          0
          • F fjdiewornncalwe

            My hunch is that the error isn't coming from your own code. Are you running this code in a web application? If so, then maybe, but if your application isn't a web application then the likely source of the 500 error is the site you are trying to hit. If the source site is working properly when you enter your full url into a browser, then you may have an issue with how your url is constructed as well.

            I wasn't, now I am, then I won't be anymore.

            E Offline
            E Offline
            Enochs
            wrote on last edited by
            #5

            Yes, my site is an asp.net web application. The error occurs anytime I try to load something using XDocument.load() that isn't in proper XML format. Wrapping it in a try block like I show above does no good. You can't validate the XML against an XML schema util you have it loaded...kind of a catch 22 in my case. I don't like the idea that XDocument(url) can stop my web-application from functioning (internal 500 error). The error does not come from my web-app code, it comes from the server running my web-application anytime I try to load fouled-up XML. My hack has it working for now but I am certain that I am overlooking something simple. I guess if there were a simple fix, someone would have posted it by now. Thanks again,

            1 Reply Last reply
            0
            • E Enochs

              I use a web based look-up service to obtain detailed information on IP addresses. It works great until the service goes down and the URL fails to render proper XML. The error (when the service is down) occurs on the "xDoc = XDocument.Load(url)" statement but does NOT throw an XmlException that I can catch...instead the server (I use a host) locks up with an internal (500) error. Since this error is from the server and not my application, I get no stack trace. All of my XML validation methods are predicated on the fact that I have the XDocument loaded first....then validate against an XML schema. But it's the load that fails so I have no chance to validate the XML. Any Ideas?

              string url = String.Format("http://www.ipgp.net/api/xml/{0}/{1}", sourceIP, password);
              try
              { /* This returns an internal server
              * error (500) if the url fails to
              * render proper xml. No error
              * gets thrown. */
              xDoc = XDocument.Load(url);
              }
              catch (XmlException ex)
              {
              /* This is a safe xml document
              * that I would like to load if
              * the load fails. */
              xDoc = XDocument.Load(safe);
              }

              UPDATE: I figured out this UGLY hack to get around the problem for now. The following code first reads the webpage into a variable and then checks the variable for an XML tag that I know should be present. If XML tag can't be found then my default (safe) xml loads. I hope someone can PLEASE give me a better fix...thanks

              HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(url);
              myRequest.Method = "GET";
              WebResponse myResponse = myRequest.GetResponse();
              StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
              string result = sr.ReadToEnd();
              sr.Close();
              myResponse.Close();

              if (ExactMatch(result, ""))
              xDoc = XDocument.Load(url);
              else
              xDoc = XDocument.Load(Safe);

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Enochs wrote:

              catch (XmlException ex)

              What makes you think that that is the only possible exception that can be thrown? If another exception is thrown then it is going to result in some error response to the client (which is what you are seeing.) Also maybe your url in the catch is failing. Also shouldn't you log something if it fails?

              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