Click here to Skip to main content
13,046,375 members (120,673 online)
Rate this:
Please Sign up or sign in to vote.
hi all,

how do i get error code from catch (Exception ex), say i got error "The request failed. The remote server returned an error: (401) Unauthorized."

i want error code 401 only.

Posted 18-Nov-12 20:58pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can try to catch WebException.

catch (WebException ex)
  HttpWebResponse resp = (HttpWebResponse)ex.Response;
  if (resp!= null)
   int httpCode = (int)response.StatusCode;

Hope that helps. if it does, mark it as answer/upvote.

kkakadiya 19-Nov-12 4:36am
i am working on c# desktop application. it doesnt allow me response property of ex at HttpWebResponse resp = (HttpWebResponse)ex.Response;
Milind Thakkar 19-Nov-12 4:43am
There is no reason why it won't allow it in desktop application. Check

There must be some other issue in the code. Pleas check or post code snippet you are trying.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


what is the authentication type you are using and what type of an application are you working on?

Milind Thakkar 19-Nov-12 4:03am
THis is a comment and not solution !!
kkakadiya 19-Nov-12 4:34am
i am working on c# desktop application and if username and password donot match then i want that authentication error code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100