İngilizce dilinde oku

Aracılığıyla paylaş


WebException.Response Özellik

Tanım

Uzak konağın döndürdüğünü yanıtı alır.

C#
public System.Net.WebResponse Response { get; }
C#
public System.Net.WebResponse? Response { get; }

Özellik Değeri

İnternet kaynağından bir yanıt varsa, bir WebResponse İnternet kaynağından gelen hata yanıtını içeren bir örnek; aksi takdirde, null.

Örnekler

Aşağıdaki örnek, özelliğini denetler Status ve temel alınan HttpWebResponse örneğin ve StatusDescription konsolunu yazdırırStatusCode.

C#
try {
   // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");

    // Get the associated response for the above request.
     HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
}
catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
                        "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
}
catch(Exception e) {
    Console.WriteLine(e.Message);
}

Açıklamalar

HTTP gibi bazı İnternet protokolleri, protokol düzeyinde bir hata oluştuğunu belirten aksi takdirde geçerli yanıtlar döndürür. Bir İnternet isteğine verilen yanıt bir hata gösterdiğindeStatus, WebRequest.GetResponse özelliğini olarak WebExceptionStatus.ProtocolError ayarlar ve oluşan özelliğinin özelliğinde Response hata iletisini içeren değerini WebException sağlarWebResponse. Uygulama, gerçek hatayı belirlemek için öğesini WebResponse inceleyebilir.

Şunlara uygulanır

Ürün Sürümler
.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