Click here to Skip to main content
12,073,837 members (68,193 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.

thanks,
krunal
Posted 18-Nov-12 21:58pm
kk20141.9K
Rate this: bad
 
good
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.

Thanks
Milind
  Permalink  
Comments
kkakadiya 19-Nov-12 4:36am
   
hi,
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
http://msdn.microsoft.com/en-us/library/system.net.webexception.response(VS.90).aspx

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

Solution 1

Hi,

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

Thanks.
  Permalink  
Comments
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
Web04 | 2.8.160208.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
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