Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Does my HttpWebRequest Return 400 Bad request?

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);                
    }
}
like image 491
user74373 Avatar asked Mar 31 '09 17:03

user74373


People also ask

What is HttpWebRequest C#?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.


1 Answers

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)";
like image 187
BFree Avatar answered Oct 24 '22 08:10

BFree