in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

Kyle Kelin on .Net

November 2007 - Posts

  • Trying to set the StatusDescription property of the Response object

    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)

      146             {

      147                 // send the error in the reponse

      148                 response.StatusDescription = e.StatusDescription;

      149                 response.StatusCode = (int)e.StatusCode;

      150 

      151                 return;

      152             }

    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 =

       76                 (HttpWebRequest)HttpWebRequest.Create(downloadUri);

       77 

       78             HttpWebResponse httpWebResponse = null;

       79 

       80             // download the file

       81             try

       82             {

       83                 httpWebResponse = httpRequest.GetResponse() as HttpWebResponse;

       84             }

       85 

       86             catch (WebException webException)

       87             {

       88                 httpWebResponse = webException.Response as HttpWebResponse;

       89 

       90                 ErrorMessage.Text = httpWebResponse.StatusCode;

       91             }

    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?

2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems