1
    XDocument xDoc = new XDocument();
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.Timeout = 1000 * 60 * 5;
    WebResponse res = req.GetResponse();
    Stream responseStream = res.GetResponseStream();
    xDoc = XDocument.Load(responseStream);
    responseStream.Close();

I am trying to use the above code to load a uri into an xdocument. I am using the HttpWebRequest and WebResponse to avoid the timeout error.

Now the problem is that most of the times the code does work but at the point where I was getting a "timeout" error before, now I am facing an "Internal server error (500)" when trying to use the above code. Any clues as to how to solve this issue? Code examples would be of great help.

Thanks!

Navyseal
  • 891
  • 1
  • 13
  • 36
  • 1
    Are there any entries in the event log? – ChrisBint Dec 22 '11 at 09:57
  • 3
    An exception is being thrown, quite possibly by `GetResponse()`. You need to find out what the exception is. Oh, and you should also use a `using` statement for the `WebResponse` and the response stream. – Jon Skeet Dec 22 '11 at 09:57
  • @JonSkeet...yes indeed the error is caused at GetResponse() – Navyseal Dec 22 '11 at 10:10
  • Have you tried visiting the URL in a browser and checking the Event Logs on that server (if you can)? Also, you should call res.Close() after your responseStream.Close(). – JamieSee Dec 22 '11 at 19:24
  • I am having the same problem, Could you please tell me that you solved it or not? – Matin Habibi Jun 03 '12 at 15:15

1 Answers1

0

I might be mistaken, but the Http 500 Interal server error is being generated by the server hosting the uri that you call. Your code might be correct, but the server is returning an error. You need to debug the server script (if you control it) or alternatively handle the http 500 error in your code.

You should call the uri directly from a browser to see if that triggers the http 500 internal server error. If so (and you control the web server) you could check the server log for more details.

Hope this helps.

Liam
  • 129
  • 4