问题描述
我使用HttpWebRequest和我得到的时候执行的GetResponse()误差
我使用此代码:
私人无效的button1_Click(对象发件人,EventArgs五)
{
乌里myUri =新的URI(http://www.google.com/sorry/ ?继续= HTTP://www.google.com/search%3Fq%3Dyamaha);
//创建一个HttpWebRequest的指定的URL对象。
HttpWebRequest的myHttpWebRequest =(HttpWebRequest的)WebRequest.Create(myUri);
//设置用户代理,好像我们是一个网络浏览器
myHttpWebRequest.UserAgent = @的Mozilla / 5.0(视窗; U; Windows NT的5.1; EN-US; rv中:1.8.0.4)壁虎/ 20060508火狐/ 1.5.0.4
HttpWebResponse myHttpWebResponse =(HttpWebResponse)myHttpWebRequest.GetResponse();
VAR流= myHttpWebResponse.GetResponseStream();
变种读者=新的StreamReader(流);
变种HTML = reader.ReadToEnd();
//释放响应对象的资源。
myHttpWebResponse.Close();
textBox1.Text = HTML;
}
服务器确实返回503 HTTP状态代码。然而,它也返回503错误状态沿着响应体(你在浏览器中看到,如果你打开一个URL的内容)。
您可以访问响应在异常的响应
属性(如果有一个503的响应,这是引发的异常是引发WebException
,其中有一个响应
属性)。你需要捕获这个异常,并妥善处理。
具体,你的代码看起来是这样的:
字符串的html;
试
{
变种myUri =新的URI(http://www.google.com/sorry/?continue=http://www.google.com/搜索%3Fq%3Dyamaha);
//创建一个HttpWebRequest的指定的URL对象。
VAR myHttpWebRequest =(HttpWebRequest的)WebRequest.Create(myUri);
//设置用户代理,好像我们是一个网络浏览器
myHttpWebRequest.UserAgent = @的Mozilla / 5.0(视窗; U; Windows NT的5.1; EN-US; rv中:1.8.0.4)壁虎/ 20060508火狐/ 1.5.0.4
VAR myHttpWebResponse =(HttpWebResponse)myHttpWebRequest.GetResponse();
VAR流= myHttpWebResponse.GetResponseStream();
变种读者=新的StreamReader(流);
HTML = reader.ReadToEnd();
//释放响应对象的资源。
myHttpWebResponse.Close();使用
}
赶上(引发WebException前)
{
(VAR SR =新的StreamReader(ex.Response.GetResponseStream()))
HTML = sr.ReadToEnd( );
}
textBox1.Text = HTML;
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 Response
property (in case there's a 503 response, the exception that's raised is a WebException
, which has a Response
property). 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;
这篇关于HttpWebRequest的错误:503服务器不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!