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