简体   繁体   English

HttpWebRequest错误:503服务器不可用

[英]HttpWebRequest Error: 503 server unavailable

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;
    }

The server really returns a 503 HTTP status code. 服务器确实返回503 HTTP状态代码。 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错误条件(如果打开该URL,则会在浏览器中看到的内容)。

You have access to the response in the exception's Response property (in case there's a 503 response, the exception that's raised is a WebException , which has a Response property). 您可以访问异常的Response属性中的Response (如果有503响应,则引发的异常是WebException ,它具有Response属性)。 you need to catch this exception and handle it properly 你需要捕获这个异常并正确处理它

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpListener服务器返回错误:(503)服务不可用 - HttpListener server returns an error: (503) Service Unavailable 如何克服 503 服务器不可用错误? - How to overcome from 503 Server unavailable error? 来自WebClient的错误:“远程服务器返回错误:(503)服务器不可用” - Error from WebClient: “The remote server returned an error: (503) Server Unavailable” 远程服务器返回错误:(503)服务器不可用。 在谷歌翻译 - The remote server returned an error: (503) Server Unavailable. in Google Translator 避免 ElasticSearch 错误 503 服务器不可用:使用 WaitForStatus - Avoiding ElasticSearch error 503 Server Unavailable: Use of WaitForStatus Whille检查http状态代码得到错误提示-远程服务器返回错误:(503)服务器不可用 - whille checking http status code getting error saying - The remote server returned an error: (503) Server Unavailable 使用Google.GData.Apps的异常:远程服务器返回错误:(503)服务器不可用 - Exception using Google.GData.Apps : The remote server returned an error: (503) Server Unavailable WCF:远程服务器返回错误:“(417)预期失败”和“(503)服务器不可用” - WCF: The remote server returned an error: '(417) Expectation Failed' and '(503) Server Unavailable' EWS Api 给出超时和 (503) 服务器不可用 - EWS Api gives timeout and (503) Server Unavailable HTTP 错误 503。该服务在 IIS 中不可用? - HTTP Error 503. The service is unavailable in IIS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM