Why is my HttpWebRequest Return 400 a bad request?

The following code crashes with an exceptional error of 400 requests. My network connection is good and I can go to the site, but I can not 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); } } 
+7
source share
4 answers

First, send WebRequest to HttpWebRequest as follows:

 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)"; 
+13
source

Install UserAgent and Referer in HttpWebRequest :

 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); } 
+5
source

There can be many reasons for this problem. Do you have more details on web exception?

One of the reasons that I came across before is that you have a line with a bad user agent. Some websites (such as Google) verify that requests come from well-known user agents to prevent bots from automatically being used on their pages.

In fact, you can verify that the user agreement for YouTube does not stop you from doing what you are doing. If so, then what you are doing can be better accomplished by going through approved channels such as web services.

+4
source

Perhaps you have a proxy server running and you have not set the Proxy property for HttpWebRequest?

+3
source

All Articles