2012-04-11 32 views
6

मैं HttpWebRequest का उपयोग कर रहा हूं और GetResponse() निष्पादित करते समय मुझे त्रुटि मिलती है।HttpWebRequest त्रुटि: 503 सर्वर अनुपलब्ध

मैं इस कोड का उपयोग:

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

आप एक ही त्रुटि मिली थी जब एक ब्राउज़र में URL या कर्ल जैसे किसी उपकरण का अनुरोध? – jlafay

+1

यह प्रोग्रामेटिक रूप से लाने के लिए एक निश्चित रूप से अजीब यूआरएल जैसा दिखता है। इसके लिए कोई कारण? –

+1

http://www.google.com/sorry/ 503 देता है। यदि आप Google को बड़ी संख्या में प्रश्न स्वचालित करने की कोशिश कर रहे हैं, तो आपको वह यूआरएल मिल सकता है। लेकिन जैसा कि जॉन स्कीट ने पूछा, आप उस यूआरएल को पहले स्थान पर क्यों सबमिट कर रहे हैं? Http://support.google.com/websearch/bin/answer.py?hl=hi&answer=86640 –

उत्तर

11

सर्वर वास्तव में एक 503 HTTP स्थिति कोड लौटाता। हालांकि, यह 503 त्रुटि स्थिति (यदि आप उस यूआरएल को खोलते हैं तो ब्राउज़र में दिखाई देने वाली सामग्री) के साथ एक प्रतिक्रिया निकाय भी देता है।

आप अपवाद के Response संपत्ति में जवाबी कार्रवाई के लिए उपयोग कर सकते है (मामले में एक 503 प्रतिक्रिया है वहाँ, लेकिन एक अपवाद उठाया है एक WebException है, जो एक Response संपत्ति है)। आप इस अपवाद को पकड़ने और Concretely ठीक से इसे संभाल

की जरूरत है, अपने कोड ऐसा दिखाई दे सकता:

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

यह कोड काम करता है .. आपको बहुत धन्यवाद –

+1

@ ऐनुन नुहा मैं थाईलैंड से अंग्रेजी में पाठ का अनुवाद करने की कोशिश कर रहा हूं लेकिन मुझे इसी तरह का मुद्दा सामना करना पड़ रहा है। मुझे GetResponse() पर अपवाद मिलता है जिसे कैच() ब्लॉक में पकड़ा जा रहा है। लेकिन यह सामग्री "वेब पेज अवरुद्ध" सामग्री के साथ पूर्ण पृष्ठ का HTML भेजता है। मैं अंग्रेजी में अनुवादित स्ट्रिंग कैसे प्राप्त कर सकता हूं। – RSB

संबंधित मुद्दे