Click here to Skip to main content
11,796,393 members (70,789 online)
Rate this: bad
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 at 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 at 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 at 19-Nov-12 4:03am
THis is a comment and not solution !!
kkakadiya at 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
0 Maciej Los 530
1 Abhinav S 392
2 OriginalGriff 340
3 CPallini 269
4 KrunalRohit 249
0 OriginalGriff 2,012
1 Maciej Los 1,725
2 KrunalRohit 1,310
3 CPallini 1,055
4 ppolymorphe 858

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2015
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