I am connecting to a webservice in my project by entereing user credentials(user name and password) i need to catch a unauthorized exception when the user enter invalid username/password. How can i do that
            Asked
            
        
        
            Active
            
        
            Viewed 3,289 times
        
    2 Answers
1
            Are you attempting to catch an exception before calling the webservice?
try
{
    result = Service.GetResult(param1, param2);
}
catch(System.Net.WebException ex)
{
    Logger.WriteError("Error calling Webservice: ", ex.ToString());
}
WebException will catch server return codes as errors I believe, such as HTTP status 404: Not Found etc.
 
    
    
        Peter Kelly
        
- 14,253
- 6
- 54
- 63
- 
                    i need to catch the exception at the calling method. Is it ok if i use the below method by checking the message text and if it is "The request failed with HTTP status 401: Unauthorized." i will have my custom message – SSK Apr 28 '10 at 02:54
- 
                    You could do that. You could then rethrow a custom exception and wrap the original exception into it - something like UnauthorizedWebException perhaps? – Peter Kelly Apr 28 '10 at 16:58
- 
                    Do you also control the Webservice? i.e. can you make changes to it and re-release it? – Peter Kelly Apr 29 '10 at 16:37
0
            
            
        Is your web service SOAP? Are you using WCF on on the service side? If so, take a look at a Specifying and Handling Faults
 
    
    
        bradjive
        
- 1,482
- 2
- 13
- 17
