C# 为什么我的 HttpWebRequest 返回 400 Bad request?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/702079/
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-04 14:02:48  来源:igfitidea点击:

Why Does my HttpWebRequest Return 400 Bad request?

c#httpwebrequest

提问by user74373

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.

以下代码因 400 错误请求异常而失败。我的网络连接良好,可以访问该站点,但无法通过 HttpWebRequest 获取此 uri。

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

采纳答案by BFree

First, cast the WebRequest to an HttpWebRequest like this:

首先,将 WebRequest 转换为 HttpWebRequest,如下所示:

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)";

回答by John Saunders

Maybe you've got a proxy server running, and you haven't set the Proxy property of the HttpWebRequest?

也许您有一个代理服务器正在运行,而您还没有设置 HttpWebRequest 的 Proxy 属性?

回答by dustyburwell

There could be many causes for this problem. Do you have any more details about the WebException?

这个问题可能有很多原因。你有关于 WebException 的更多细节吗?

One cause, which I've run into before, is that you have a bad user agent string. Some websites (google for instance) check that requests are coming from known user agents to prevent automated bots from hitting their pages.

我之前遇到过的一个原因是您的用户代理字符串错误。一些网站(例如谷歌)会检查请求是否来自已知的用户代理,以防止自动机器人访问他们的页面。

In fact, you may want to check that the user agreement for YouTube does not preclude you from doing what you're doing. If it does, then what you're doing may be better accomplished by going through approved channels such as web services.

事实上,您可能需要检查 YouTube 的用户协议是否不妨碍您做您正在做的事情。如果是这样,那么通过 Web 服务等已获批准的渠道可能会更好地完成您所做的工作。

回答by Konstantin Tarkus

Set UserAgentand Refererin your HttpWebRequest:

HttpWebRequest 中设置UserAgentReferer

var request = (HttpWebRequest)WebRequest.Create(@"http://www.youtube.com/");
request.Referer = "http://www.youtube.com/"; // optional
request.UserAgent =
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; " +
    "Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; " +
    ".NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; " +
    "InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)";
try
{
    var response = (HttpWebResponse)request.GetResponse();
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        var html = reader.ReadToEnd();
    }
}
catch (WebException ex)
{
    Log(ex);
}