2012-04-11 42 views
6

HttpWebRequest kullanıyorum ve GetResponse() çalıştırılırken hata alıyorum. HttpWebRequest Hata: 503 sunucu kullanılamıyor

Bu kodu kullanarak:

private void button1_Click(object sender, EventArgs e) 
    { 
     Uri myUri = new Uri("http://www.google.com/sorry/?continue=http://www.google.com/search%3Fq%3Dyamaha"); 
     // Create a 'HttpWebRequest' object for the specified url. 
     HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri); 
     // Set the user agent as if we were a web browser 
     myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4"; 

     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
     var stream = myHttpWebResponse.GetResponseStream(); 
     var reader = new StreamReader(stream); 
     var html = reader.ReadToEnd(); 
     // Release resources of response object. 
     myHttpWebResponse.Close(); 

     textBox1.Text = html; 
    } 
+0

aynı hata aldınız bir tarayıcıda URL veya kıvrılma gibi bir araç isterken? – jlafay

+1

Programlı olarak almak için kesinlikle garip bir URL gibi görünüyor. Bunun bir sebebi var mı? –

+1

http://www.google.com/sorry/, 503 değerini döndürür. Çok sayıda soruyu Google'a otomatik hale getirmeye çalışıyorsanız, bu URL'yi alabilirsiniz. Ancak Jon Skeet'in sorduğu gibi, neden ilk etapta bu URL'ye istek gönderiyorsunuz? Http://support.google.com/websearch/bin/answer.py?hl=tr&answer=86640 –

cevap

11

sunucu gerçekten bir 503 HTTP durum kodunu döndürür. Ancak, 503 hata koşuluyla birlikte bir yanıt gövdesi de döndürür (bu URL'yi açarsanız bir tarayıcıda gördüğünüz içerikler).

Sen istisnanın Response özelliğinde tepki erişebilir (503 yanıt var durumda yükseltilmiş oluyor istisnası Response mülkü bulunan bir WebException gelir). Bu durum yakalamak ve Somut düzgün

ele gerekir, kod aşağıdaki gibi görünebilir:

string html; 

try 
{ 
    var myUri = new Uri("http://www.google.com/sorry/?continue=http://www.google.com/search%3Fq%3Dyamaha"); 
    // Create a 'HttpWebRequest' object for the specified url. 
    var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri); 
    // Set the user agent as if we were a web browser 
    myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4"; 

    var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
    var stream = myHttpWebResponse.GetResponseStream(); 
    var reader = new StreamReader(stream); 
    html = reader.ReadToEnd(); 
    // Release resources of response object. 
    myHttpWebResponse.Close(); 
} 
catch (WebException ex) 
{ 
    using(var sr = new StreamReader(ex.Response.GetResponseStream())) 
     html = sr.ReadToEnd(); 
} 

textBox1.Text = html; 
+0

bu kod çalışıyor .. çok teşekkür ederim –

+1

@Ainun Nuha Tayland'dan İngilizceye metin çevirmeye çalışıyorum ama benzer sorunlarla karşılaşıyorum. Catch() bloğunda yakalanan GetResponse() uygulamasında istisna yapıyorum. Ancak, "Sayfanın Web Sayfası Engellendi" içerikli tam sayfanın HTML'sini gönderir. Dizeyi ingilizce'ye nasıl çevirebilirim? – RSB

İlgili konular