During my latest project I had to create a HttpRequest manually to test a HttpHandler that I wrote. I wanted to go over some of the basics of the code and write about a strange error that I ran across. The HttpHandler is going to return a response code (200) if it is successful and some other number if the process fails. For example a 401 error if the user isn't authorized to view the request that was submitted. Nothing new or difficult there. The problem occurs because I want to set the StatusDescription property of the Response to the Exception message in the HttpHandler. The Exception message has some specific information about why the request failed. This way the client of the Handler can use the StatusDescription property to return a useful error message to the user.
145 catch (ExtractRetrievalException e)
147 // send the error in the reponse
148 response.StatusDescription = e.StatusDescription;
149 response.StatusCode = (int)e.StatusCode;
In the client I create a httpRequest Object and call the GetResponse method. Then in the catch block I try and set the ErrorMessage text property to StatusDescription to display the message.
75 HttpWebRequest httpRequest =
78 HttpWebResponse httpWebResponse = null;
80 // download the file
83 httpWebResponse = httpRequest.GetResponse() as HttpWebResponse;
86 catch (WebException webException)
88 httpWebResponse = webException.Response as HttpWebResponse;
90 ErrorMessage.Text = httpWebResponse.StatusCode;
The property gets set correctly in the Handler but when I access it in the client it is set to "Unauthorized" instead. The reason it is Unathorized is because the StatusCode being sent back in the Response object is 401 but why has it changed. The only reason I can think of is IIS is reseting that property. So far I have came up short on Google. Has anyone came across something like this?