buynsell 0 Newbie Poster

After bunch of searching on google I end up with posting new thread for help,

I am trying to login to youtube using webRequest/Response but no success, it is returning source code which tells that "Your browser cookies disabled ..." etc.

Attaching my code and can someone tell me the solution or if any better way to do this ?

I know that youtube api offer these function but I've to do without api for my project.

here is my source code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                CookieContainer container = new CookieContainer();
                request.CookieContainer = container;

                StreamReader reader;
                Stream stream;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                    stream = response.GetResponseStream();
                    reader = new StreamReader(stream);

                    string temp1 = reader.ReadToEnd();
                    richTextBox1.Text = temp1;
                    
                    String Pattern = "name=\"GALX\"[\\s]*value=\"([a-zA-Z0-9-_\\.]*)\"";
                    galx = richTextBox1.Text;
                    Match myMatch = Regex.Match(galx, Pattern);
                    galx = myMatch.Groups[1].ToString();

                    string url2 = "https://www.google.com/accounts/ServiceLoginAuth?service=youtube&ltmpl=sso&continu e=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_h andle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_U S%26next%3D%252F&service=youtube&uilel=3&ltmpl=sso &hl=en_US&ltmpl=sso&GALX=" + galx + "&Email=username&Passwd=password&rm Shown=1&signIn=Sign+in&asts=";

                    richTextBox1.Text = "";

                    HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2);
                    request2.CookieContainer = container;
                    request2.Method = "POST";
                    request2.Referer = "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true& continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Fa ction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl %3Den_US%26next%3D%252F&hl=en_US&ltmpl=sso";
                    request2.MaximumAutomaticRedirections = 50;                    
                    request2.AllowAutoRedirect = true;
                    request2.KeepAlive = true;
                    request2.ContentLength = 0;
                    container = request2.CookieContainer;

                    HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
                    response2.Cookies = request2.CookieContainer.GetCookies(request2.RequestUri);


                    Stream responseStream = response2.GetResponseStream();
                    response2.Cookies = request2.CookieContainer.GetCookies(request2.RequestUri);
                    request2.CookieContainer = container;

                    StreamReader ressponseReader = new StreamReader(response2.GetResponseStream());

                    string temp;
                    temp = ressponseReader.ReadToEnd().ToString();
                    richTextBox2.Text += temp;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.