C# HttpWebRequest 错误:503 服务器不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:27:36  来源:igfitidea点击:

HttpWebRequest Error: 503 server unavailable

c#httpshttpwebrequest

提问by Ainun Nuha

I'm using HttpWebRequest and I get error when execute GetResponse().

我正在使用 HttpWebRequest 并且在执行 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;
    }

采纳答案by Cristian Lupascu

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).

服务器确实返回了 503 HTTP 状态代码。但是,它还返回响应正文以及 503 错误条件(如果您打开该 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 a WebException, which has a Responseproperty). you need to catch this exception and handle it properly

您可以访问异常Response属性中的响应(如果有 503 响应,引发的异常是 a WebException,它有一个Response属性)。您需要捕获此异常并正确处理它

Concretely, your code could look like this:

具体来说,您的代码可能如下所示:

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;