2012-03-01 38 views
9

Bazı dahili web uygulamalarının durumunu kontrol etmek için bir uygulama yazıyorum. Bu uygulamalardan bazıları Windows kimlik doğrulamasını kullanır. Durumu kontrol etmek için bu kodu kullandığımda, The remote server returned an error: (401) Unauthorized.'u atar. Bu anlaşılabilir bir durumdur, çünkü webiste herhangi bir kimlik sağlamadım, bu yüzden yetkili değilim.
Web sitesi durum bilgisi almak için 401 yetkisiz hata nasıl yoksayılır

WebResponse objResponse = null; 
WebRequest objRequest = HttpWebRequest.Create(website); 
objResponse = objRequest.GetResponse(); 


böyle bir şey yapmadan 401 hatayı görmezden bir yolu var mı?

WebRequest objRequest = HttpWebRequest.Create(website); 

try 
{ 
    objResponse = objRequest.GetResponse(); 
} 
catch (WebException ex) 
{ 
    //Catch and ignore 401 Unauthorized errors because this means the site is up, the app just doesn't have authorization to use it. 
    if (!ex.Message.Contains("The remote server returned an error: (401) Unauthorized.")) 
    { 
     throw; 
    }      
} 
+0

Burada 'göz ardı' ile ne demek istiyorsun, sonraki sayfaya geç? –

+1

Muhtemelen istisnadan hiçbir şekilde faydalanmayacaktır, ancak daha hafif olduğu ve bu durumu iyi işlediği için HEAD talebinde bulunmalısınız. –

+0

@HenkHolterman Web sorgularını bir istisna atmamayı veya yalnızca yukarıdaki gibi bir istisnai yutmayı sağlayan bir özellik. – guanome

cevap

3

sunucu aşağı ya da bir zaman aşımı özel alacak erişilemiyor . Bunu halletmenin tek yolunun bir deneme/yakalama ile olduğunu biliyorum.

Çoğu hata için durumun böyle olduğundan (401/404/501) eminim, bu nedenle: Hayır, istisnaları görmezden gelemezsiniz (engellemez), ancak bunları işlemeniz gerekir. Uygulamanızın bulunduğu StatusCodes'ların çoğunu elde etmenin tek yolu bunlar.

2

Bunun kısa açıklaması, durum kodu için myHttpWebResponse.StatusCode'u kontrol etmek ve buna göre hareket etmek olacaktır. reference den

örnek kod:

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 occured : {0}",e.Status); 
      } 
     catch(Exception e) 
     { 
      Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); 
     } 
    } 
+2

İstisna 'objResponse = objRequest.GetResponse();' şeklinde atılır, bu yüzden yanıtı hiçbir zaman ayarlamayacağından yanıtı kontrol edemiyorum. Eğer istisnai durumdaysa, bana bir 'ProtocolError' verecektir, bu yüzden durum kodunun gerçekte ne olduğunu görmek için istisna mesajını gözden geçirmem gerekecek. – guanome

18

Bunu denemek için öneririm:

 try 
     { 
      objResponse = objRequest.GetResponse() as HttpWebResponse; 
     } 
     catch (WebException ex) 
     { 
      objResponse = ex.Response as HttpWebResponse; 
     } 
     finally 

WebException tepkisini istediğiniz tüm bilgileri vardır.

İlgili konular