> Hi, I have a web site that returns a 401 (access denied) if the login
> details are incorrect. WebRequest.GetResponse correctly throws this
[quoted text clipped - 6 lines]
> 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
Mark - 21 Jan 2007 19:32 GMT
> > Hi, I have a web site that returns a 401 (access denied) if the login
> > details are incorrect. WebRequest.GetResponse correctly throws this
[quoted text clipped - 20 lines]
>
> 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.
David Browne - 22 Jan 2007 04:27 GMT
>> > Hi, I have a web site that returns a 401 (access denied) if the login
>> > details are incorrect. WebRequest.GetResponse correctly throws this
[quoted text clipped - 25 lines]
> 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
Mark - 23 Jan 2007 09:08 GMT
> Odd.
>
[quoted text clipped - 28 lines]
>
> 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...