Error handling of specific HTTP response codes C# with Example



Error handling of specific HTTP response codes C# with Example

(such as 404 Not Found) 
using System.Net; 
... 
string serverResponse; 
try 
{ 
// Call a method that performs an HTTP request (per the above examples). 
serverResponse = PerformHttpRequest(); 
} 
catch (WebException ex) 
{ 
if (ex.Status == WebExceptionStatus.ProtocolError) 
{ 
HttpWebResponse response = ex.Response as HttpWebResponse; 
if (response != null) 
{ 
if ((int)response.StatusCode == 404) // Not Found 
{ 
// Handle the 404 Not Found error 
// ... 
} 
else 
{ 
// Could handle other response.StatusCode values here. 
// ... 
} 
} 
} 
else 
{ 
// Could handle other error conditions here, such as WebExceptionStatus.ConnectFailure. 
// ... 
} 
} 

0 Comment's

Comment Form

Submit Comment