Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 21, 2015 10:48 PM
Ok I've been going at this a few hours now and can't figure this out.
I'm doing a simple web request to a 3rd party API using the Windows.Web.Http.HttpClient class.
Occasionally the API will return an Error 500 (Internal Server Error), which is normal apparently. According to my API documentation:
"On an error from the service, the service will return a 500 HTTP status with XML content of the following format: "
<error>
<msg>$(MESSAGE)</msg>
</error>
My question is, how do I get this response message?
A simplified example of my code would be:
private async static Task<string> GetString(Uri uri)
{
var httpClient = new Windows.Web.Http.HttpClient();
string xml;
// ... Setup httpClient, add headers etc
try
{
xml = await httpClient.GetStringAsync(uri);
}
catch
{
// Error 500 throws an exception
}
return xml;
}
All replies (7)
Wednesday, July 22, 2015 10:47 PM âś…Answered
For anyone who might be curious, I've change my code to something like this and seems to work.
response = await httpClient.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
xml = await response.Content.ReadAsStringAsync();
}
else
{
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
// Try to read response, log error etc.
var errorxml = await response.Content.ReadAsStringAsync();
...
}
else
{
// Do something for other errors
...
}
}
...
Thanks everyone for the help
Tuesday, July 21, 2015 10:54 PM
modify your catch block:
catch (WebException ex)
{
string response;
var stream=ex.Response.GetResponseStream();
using(var sr = new StreamReader(stream))
{
response=sr.ReadToEnd();
}
}
Wednesday, July 22, 2015 12:29 AM
<error> <msg>$(MESSAGE)</msg> </error> It's XML man. You should be able to load it into a XML Document.
Wednesday, July 22, 2015 12:36 AM
Windows.Web.Http.HttpClient doesn't appear to throw a WebException like System.Net.HttpClient does. It just throws a generic exception and there doesn't seem to be a WebException equivalent in Windows.Web
OUTPUT:
* 'ApiGet.GetString' method: Fetching from API -- Attempt 1
A first chance exception of type 'System.Exception' occurred in mscorlib.dll
WinRT information: Response status code does not indicate success: 500 (Internal Server Error).
Wednesday, July 22, 2015 12:42 AM
<error> <msg>$(MESSAGE)</msg> </error> It's XML man. You should be able to load it into a XML Document.
Yes, but the HttpClient doesn't return any XML, it just throws an exception. Where do I find this XML?
Wednesday, July 22, 2015 1:33 AM
xml = await httpClient.GetStringAsync(uri);
It should be coming back in the XML. A 500 error message may not comeback with anything meaningful, like malformed XML on the service side that some method was doing when it blew up or something else that was catastrophic that caused the 500 error to be thrown.
You may have to use Fiddler to find out what is happening by having Fiddler (free) running, see the 500 error line in Fiddler, double click the 500 error line, and start looking in the right-side pane to see if any further explanations as to why the 500 error was thrown.
You should remove the try/catch and see if anything comes back in the xml in the statement above.
Wednesday, July 22, 2015 2:00 AM
xml = await httpClient.GetStringAsync(uri);
It should be coming back in the XML. A 500 error message may not comeback with anything meaningful, like malformed XML on the service side that some method was doing when it blew up or something else that was catastrophic that caused the 500 error to be thrown.
You may have to use Fiddler to find out what is happening by having Fiddler (free) running, see the 500 error line in Fiddler, double click the 500 error line, and start looking in the right-side pane to see if any further explanations as to why the 500 error was thrown.
You should remove the try/catch and see if anything comes back in the xml in the statement above.
Yeah, i'm doing some fiddling now, just waiting for another 500 to come up (it only happens occasionally).
I'm looking at the MSDN docs and all the examples are using the GetStringAsync method, but I think maybe what I should be doing is calling GetAsync which returns a HttpResponseMessage and then using something like response.Content.ReadAsStringAsync() and response.StatusCode to get the info I'm after.
Just going to test this to confirm, hopefully the error xml will show up in the response content with a status code of 500 instead of throwing an exception as with GetStringAsync().