5

When I connect to a url via a web client, it returns a 500 internal server error. I can visit that URL in my browser just fine. This was working for months and the problem started yesterday. No changes on my end. I understand that a 500 status code is a server error, but I wanted to see if there was something else I could do at this point as I'm freaking out.

Example url: http://www.myfitnesspal.com/reports/printable_diary/chuckgross?from=2015-07-17&to=2015-07-17

The code:

 string results;
 using (var client = new WebClient())
 {
    results = client.DownloadString(url);
 }

This gives the exception: The remote server returned an error: 500.

Using Fiddler, I'm seeing a page returned that is their "Site Down" page with the message: "Sorry, but a server error occurred processing your request. Our team has been notified of the issue".

Is there anything that I can do given that the URL itself works fine in a browser? Any alternative ways to try to troubleshoot?

CGross
  • 493
  • 1
  • 7
  • 18
  • It could be that the url is not encoded automatically by `WebClient` which makes the `from` and `to` query string parameters invalid and throws exception on the server side. Try https://msdn.microsoft.com/en-US/library/4fkewx0t(v=vs.110).aspx and see if the problem still persists. – Khanh TO Jul 18 '15 at 13:25

3 Answers3

12

It seems they try to detect browser version on server side by querying User-Agent header but do not expect it to be missing.

To fix error on client side you can for instance fill it with one used by IE 9 before sending request:

client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)";
dewaffled
  • 2,850
  • 2
  • 17
  • 30
  • Nice, that fixed it, thank you! Just curious, if you wouldn't mind sharing how you arrived at that conclusion. Did you inspect a web client connection to the URL itself or were you able to figure that out just from what I posted? Since this just recently became an issue, any insight that I could apply to potential future problems would be great. =) – CGross Jul 18 '15 at 14:11
  • 3
    It just was the first thing I thought of. If error happens on server side for your client and does not happen for other, how server distinguish them? - Usually with `User-Agent` header. For troubleshooting, I just checked - there is `Raw` button under `Inspectors` tab in fiddler - it would allow to easily compare complete requests side by side by copy-pasting into any diff tool. – dewaffled Jul 18 '15 at 14:27
  • frymode thank you, that's helpful, I appreciate it so much! =) – CGross Jul 18 '15 at 15:01
0

It's normal to receive an exception when the server returns a HTTP error while using the WebClient class, it signals that the server couldn't complete the request successfully. That doesn't means that it didn't return a proper html response, however. You should catch the exception, where you can get the response stream object and extract the data from there.

Alejandro
  • 7,290
  • 4
  • 34
  • 59
0

Another possibility is the user's preferred language that the browser passes. That's what was causing my issue, as the API was looking for it but I wasn't providing it.

Mmm
  • 682
  • 9
  • 11