为什么我的HttpWebRequest不会返回400错误的请求? [英] Why Does my HttpWebRequest Return 400 Bad request?
本文介绍了为什么我的HttpWebRequest不会返回400错误的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下code失败,出现400错误的请求例外。我的网络连接是好的,我可以去的网站,但我不能HttpWebRequest的得到这个URI。
私人无效button3_Click(对象发件人,EventArgs的发送)
{
WebRequest的REQ = HttpWebRequest.Create(@http://www.youtube.com/);
尝试
{
//返回400错误的请求...任何想法?
WebResponse的响应= req.GetResponse();
}
赶上(引发WebException前)
{
日志(ex.Message);
}
}
解决方案
首先,施放的WebRequest到HttpWebRequest的是这样的:
HttpWebRequest的REQ =(HttpWebRequest的)HttpWebRequest.Create(@http://www.youtube.com/);
然后,加入这一行code的:
req.UserAgent =Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727);
The following code fails with a 400 bad request exception. My network connection is good and I can go to the site but I cannot get this uri with HttpWebRequest.
private void button3_Click(object sender, EventArgs e)
{
WebRequest req = HttpWebRequest.Create(@"http://www.youtube.com/");
try
{
//returns a 400 bad request... Any ideas???
WebResponse response = req.GetResponse();
}
catch (WebException ex)
{
Log(ex.Message);
}
}
解决方案
First, cast the WebRequest to an HttpWebRequest like this:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(@"http://www.youtube.com/");
Then, add this line of code:
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
这篇关于为什么我的HttpWebRequest不会返回400错误的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文