SORU
22 Aralık 2009, Salı


Nasıl bir 404 binebilirim?

Aşağıdaki kodu var:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
request.Credentials = MyCredentialCache;

try
{
    request.GetResponse();
}
catch
{
}

Nasıl özel bir 404 hatası binebilirim? Bu WebExceptionStatus.ProtocolError sadece bir hata oluştuğunu algılayabilir ama hatanın tam kodunu ver.

Örneğin:

catch (WebException ex)
{
    if (ex.Status != WebExceptionStatus.ProtocolError)
    {
        throw ex;
    }
}

Sadece yararlı değil zaten. protokol dışında 401, 503, 403, bir şey gerçekten olabilir.

CEVAP
22 Aralık 2009, Salı


HttpStatusCode Enumeration, HttpStatusCode.NotFound özel olarak kullanın

Gibi bir şey

HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound) {
  //
}

Nerede
we WebException.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ehowhome

    ehowhome

    22 NİSAN 2009
  • Murray Winiata

    Murray Winia

    2 ŞUBAT 2009
  • schmittastic

    schmittastic

    9 EYLÜL 2009