Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

You can try to catch WebException.

C#
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
 
Share this answer
 
Comments
kk2014 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;
MT_ 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
Hi,

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

Thanks.
 
Share this answer
 
Comments
MT_ 19-Nov-12 4:03am    
THis is a comment and not solution !!
kk2014 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900