PROPERTY

HttpRequestException.StatusCode

Optional status code attached to the exception. A null value means no HTTP status is recorded, not necessarily a particular transport failure. Inspect cancellation separately; do not retry based only on exception type.

Formal Syntax & Structure

csharp syntaxcsharp
HttpStatusCode? code = exception.StatusCode;

Working Example

Example Snippetcsharp
using System;using System.IO;using System.Collections.Generic;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Threading;var ex = new HttpRequestException("Synthetic",null,HttpStatusCode.BadGateway);Console.WriteLine((int?)ex.StatusCode); // 502
C# & .NET Reference