Getting a response from a WebRequest with an exception?

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi, I have a web site that returns a 401 (access denied) if the login
details are incorrect. WebRequest.GetResponse correctly throws this
exception and I handle it, so no problems there. However, the web site
also provides additional information with the 401 (e.g. "another
administrator is currently logged in", or "password is incorrect but
user name is correct" and so on). However, because
WebRequest.GetResponse has already thrown, the response stream is null.
Is there a way to access the response even if an exception has been
thrown? (Apart from a raw TCP connection on port 80, that is!) I've
looked at the exception instance's Response property to no avail.

TIA
Mark
 
Mark said:
Hi, I have a web site that returns a 401 (access denied) if the login
details are incorrect. WebRequest.GetResponse correctly throws this
exception and I handle it, so no problems there. However, the web site
also provides additional information with the 401 (e.g. "another
administrator is currently logged in", or "password is incorrect but
user name is correct" and so on). However, because
WebRequest.GetResponse has already thrown, the response stream is null.
Is there a way to access the response even if an exception has been
thrown? (Apart from a raw TCP connection on port 80, that is!) I've
looked at the exception instance's Response property to no avail.

In your catch, examine WebException.Response.

From HttpWebRequest Class

The HttpWebRequest class throws a WebException when errors occur while
accessing a resource. The WebException.Status property contains a
WebExceptionStatus value that indicates the source of the error. When
WebException.Status is WebExceptionStatus.ProtocolError, the Response
property contains the HttpWebResponse received from the resource.

http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

David
 
David said:
In your catch, examine WebException.Response.

From HttpWebRequest Class

The HttpWebRequest class throws a WebException when errors occur while
accessing a resource. The WebException.Status property contains a
WebExceptionStatus value that indicates the source of the error. When
WebException.Status is WebExceptionStatus.ProtocolError, the Response
property contains the HttpWebResponse received from the resource.

http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

David

As I said, it is null. (I guess the documentation is wrong because the
status is ProtocolError, but the Response is null.) Any other ideas? I
could wrap my own web parser, but that seems an awful lot of effort for
something so trivial.
 
Mark said:
As I said, it is null. (I guess the documentation is wrong because the
status is ProtocolError, but the Response is null.) Any other ideas? I
could wrap my own web parser, but that seems an awful lot of effort for
something so trivial.

Odd.

The following works for me:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;


namespace Test
{
public class Dictionary
{
public static void Main()
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://localhost/Fobidden");
try
{
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
HttpWebResponse error = (HttpWebResponse) ex.Response;
Console.WriteLine((int)error.StatusCode);
}
Console.ReadKey();
}
}
}

David
 
David said:
Odd.

The following works for me:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;


namespace Test
{
public class Dictionary
{
public static void Main()
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://localhost/Fobidden");
try
{
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
HttpWebResponse error = (HttpWebResponse) ex.Response;
Console.WriteLine((int)error.StatusCode);
}
Console.ReadKey();
}
}
}

David

Oh dear - I'm displaying my stupidity for the whole world to see and I
am clearly depriving a village of its idiot! You're right - it work
fine. I had a wrapper class around the re.GetResponse() code and I had
"forgotten" to assign the Response property in my wrapper when catching
the exception - so null was being passed via the property instead.
Thanks for your help. I'll get my coat...
 
Back
Top