Link to home
Start Free TrialLog in
Avatar of GVNPublic123
GVNPublic123

asked on

YouTube Login using HttpWebRequest C#

500 points to the guy who figures this problem out with working solution (source code)!

I am trying to login YouTube using C# Httpwebrequest and I am getting mad.
I tried looking for HTTP headers YouTube uses to login, and I found some, but cant manage to make it work.
Lastly, right now I have no more clue what I should do. (I tried what I could, but I dont really understand this concept, so I leave the problem to you experts).
Here is what I have done so far (look code section). I may be doing completely wrong thing, please research on your own.

public bool Login(string username, string password, CookieContainer cookies, string useragent)
{
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    string url;
    string url2;
    try
    {    
        url2 = "&ltmpl=sso&GALX=nQYPV6vAv54&Email=" + username + "&Passwd=" + password + "&PersistentCookie=yes&rmShown=1&signIn=Sign+in&asts=";
        url = "https://www.google.com/accounts/ServiceLogin?service=youtube&hl=en_US&passive=true&ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2Fsignup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1%26next%3D%2Findex&service=youtube&uilel=3"; //+url2
        
        request = (HttpWebRequest)WebRequest.Create(url);
        request.Referer = url;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
            request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-silverlight, */*";
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.CookieContainer = cookies;
            request.Timeout = this.timeout * 0x3e8;
            request.ContentLength = url.Length;
            request.ReadWriteTimeout = this.timeout * 0x3e8;
            request.UserAgent = useragent;
            if (randomProxy != null && checkBox1.Checked == true)
            {
                //request.Proxy = new WebProxy(proxy.ToString());
                request.Proxy = new WebProxy(randomProxy.ToString());
            }
            byte[] buffer = Encoding.UTF8.GetBytes(url2);
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();
            response = (HttpWebResponse)request.GetResponse();
            
            //response = (HttpWebResponse)request.GetResponse();
            //new StreamReader(response.GetResponseStream()).ReadToEnd();
            string input = new StreamReader(response.GetResponseStream()).ReadToEnd();
            
            using (System.IO.StreamWriter output = System.IO.File.CreateText(Application.StartupPath + @"\Resources\log.txt"))
            {
                output.WriteLine(input);
            }
            return true;
    }
    catch(Exception e)
    {
        MessageBox.Show(null, e.ToString(), "");
        return false;
    }
}

Open in new window

Avatar of dungla
dungla
Flag of Viet Nam image

Hi there,

Try changing request.Method = "POST"; to request.Method = "GET";
cause passing params as query string variables
ASKER CERTIFIED SOLUTION
Avatar of GVNPublic123
GVNPublic123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prizzy
prizzy

How did you fix it?