1

I am making an API call to a REST service. The REST service returns an XML string that contains a user token if the password submitted is correct, or an XML string with data if it isn't.

Here is an example if the password is incorrect:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<authenticationResponse>
  <statusCode>403</statusCode>
  <errors>
    <error>
       ....
    </error>
  </errors>
  <timestamp>2011-03-31 22:45:03 GMT</timestamp>
</authenticationResponse>

With this code below, it appears .NET is translating this to an actual error. I still want it to read the XML data and ignore any error:

RequestData requestData = (RequestData)result.AsyncState;
HttpWebResponse response = 
                 (HttpWebResponse)requestData.Request.EndGetResponse(result);

How I can ignore the error but still create the stream to read the XML?

Kev
  • 118,037
  • 53
  • 300
  • 385
Dave
  • 1,062
  • 8
  • 28

3 Answers3

0

Your code isn't translating this into an actual error - a HTTP status code of 403 is "Forbidden".

HTTP "Not Found" has a HTTP status code of 404 so it looks like the HTTP endpoint you're requesting doesn't exist in the REST service.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • If that's the case, how come Fiddler and other REST test clients all pickup the xml, and even if I catch and read I cant? I know it's a legit error number, but I am not to sure that's a legit error. – Dave Apr 01 '11 at 01:37
  • @dave - I don't enough abut your specific implementation, so I don't know. I'm just explaining that 404 - "Not Found" <> "403" - "Forbidden" because in your post you thought that the 403 status in your XML was being translated to an actual 403. – Kev Apr 01 '11 at 07:48
0

Catch WebException, check the exception status, read the response. See these questions for examples:

Community
  • 1
  • 1
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
0

Ok so I figured it out, there's a few parts here.

First off the API is generating that xml when the parameters passed don't meet whats expected. In this case, if the password is incorrect, it'll pass back the 403.

The error is an error, so the framework treats it as such, however the error contains a response anyways, you just have to get the response off the error. This is essentially the answer to the question. Have to catch it the error and snag the response off the error to read the data in the stream, which is what Mauricio was getting to.

Essentially, I guess all the answers here are correct, or pieces of it, just took a little digging to put it all together.

Thanks Guys.

Dave
  • 1,062
  • 8
  • 28