I’m using HttpWebRequest and I get error when execute GetResponse().
I using this code:
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;
}
The server really returns a 503 HTTP status code. However, it also returns a response body along with the 503 error condition (the contents you see in a browser if you open that URL).
You have access to the response in the exception’s
Responseproperty (in case there’s a 503 response, the exception that’s raised is aWebException, which has aResponseproperty). you need to catch this exception and handle it properlyConcretely, your code could look like this: