Прочитать на английском

Поделиться через


HttpWebResponse.StatusCode Свойство

Определение

Возвращает состояние ответа.

C#
public virtual System.Net.HttpStatusCode StatusCode { get; }
C#
public System.Net.HttpStatusCode StatusCode { get; }

Значение свойства

Одно из значений перечисления HttpStatusCode.

Исключения

Текущий экземпляр удален.

Примеры

В следующем примере используется StatusCode для проверки состояния HttpWebResponse ОК.

C#
public static void GetPage(String url)
{
    try
      {	
            // Creates an HttpWebRequest for the specified URL.
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            // Sends the HttpWebRequest and waits for a response.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
               Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
                                    myHttpWebResponse.StatusDescription);
            // Releases the resources of the response.
            myHttpWebResponse.Close();
        }
    catch(WebException e)
       {
            Console.WriteLine("\r\nWebException Raised. The following error occurred : {0}",e.Status);
       }
    catch(Exception e)
    {
        Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
    }
}

Комментарии

Параметр StatusCode — это число, указывающее состояние HTTP-ответа. Ожидаемые значения состояния определяются в HttpStatusCode классе .

Применяется к

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0