How To Get Past 401?

How to ignore 401 unauthorized error from webrequest to get website status

  • I am writing an application to check the status of some internal web applications. Some of these applications use Windows authentication. When I use this code to check the status, it throws The remote server returned an error: (401) Unauthorized.. Which is understandable because I haven't provided any credentials to the webiste so I am not authorized. WebResponse objResponse = null; WebRequest objRequest = HttpWebRequest.Create(website); objResponse = objRequest.GetResponse(); Is there a way to ignore the 401 error without doing something like this? WebRequest objRequest = HttpWebRequest.Create(website); try { objResponse = objRequest.GetResponse(); } catch (WebException ex) { //Catch and ignore 401 Unauthorized errors because this means the site is up, the app just doesn't have authorization to use it. if (!ex.Message.Contains("The remote server returned an error: (401) Unauthorized.")) { throw; } }

  • Answer:

    When the server is down or unreachable you will get a timeout exception. I know that the only way to handle that is with a try/catch. I'm quite sure this is the case for most errors (401/404/501), so: No, you can't ignore (prevent) the exceptions but you will have to handle them. They are the only way to get most of the StatusCodes your App is looking for.

guanome at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I would suggest to try this: try { objResponse = objRequest.GetResponse() as HttpWebResponse; } catch (WebException ex) { objResponse = ex.Response as HttpWebResponse; } finally The WebException has the response all information you want.

Geraldo Magella Junior

The short of it is you'll want to check the myHttpWebResponse.StatusCode for the status code and act accordingly. Sample code from http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.statuscode.aspx: public static void GetPage(String url) { try { // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for a response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}", myHttpWebResponse.StatusDescription); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } }

user1169578

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.