I am using webrequests to get video data off of YouTube using the URL http://youtube.com/get_video_info?video_id={ID HERE}
When I visit the URL with a browser, it downloads the response file and all of the needed data is present. However, when I use an HttpWebRequest
to download the response with a proper UserAgent, a lot of the data is missing.
The data that isn't present mostly involves locale information, browser version info (which I thought would come with the right UserAgent string but I guess not), and ids that refer to events created from the request.
My question is: Why is this data present in the version my browser downloads versus the WebRequest? How can I see what my browser is sending that makes it differ from the request I'm creating?
Here's how I'm currently requesting the data in C#:
var url = string.Format("http://youtube.com/get_video_info?video_id={0}", videoId);
var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = userAgent;
var response = request.GetResponse();
string contents;
using (var sr = new StreamReader(response.GetResponseStream()))
contents = sr.ReadToEnd();
Standard WebRequest and browser request send different headers. You have to properly prepare your WebRequest (setup correct headers and cookies) to make the same HTTP request.
To see the difference, download Fiddler and open the same URL in browser, then in Visual Studio and compare both requests in raw text mode. You will note that WebRequest is missing a lot of headers.