Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2007

Tip: Looking for answers? Try searching our database.

Getting a response from a WebRequest with an exception?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 21 Jan 2007 13:22 GMT
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
David Browne - 21 Jan 2007 16:35 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 - 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...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.