Search code examples
c#.netgmailsystem.net.httpwebrequest

Login to Gmail using HttpWebRequest no cookie received


I want to Login into my Gmail account and change the password for 100 times to set it to my old password. (as you know google keeps the history of 100 previous passwords, and does not let you to choose one of them). I'm using HttpWebRequest and HttpWebResponse to solve my problem. and I monitored the request and response in Fiddler. But when I login to gmail with following code I don't get any cookies. Where's the problem?!

            HttpWebRequest http = WebRequest.Create("https://accounts.google.com/ServiceLoginAuth") as HttpWebRequest;
        http.KeepAlive = true;
        http.Host = "accounts.google.com";
        http.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, new TimeSpan(0));
        http.Referer = "https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail";
        http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        http.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
        http.Method = "POST";
        http.ContentType = "application/x-www-form-urlencoded";
        string postData = "";
        postData += "GALX=" + "qpld0kUdZuc";
        postData += "&continue=" + "https://mail.google.com/mail/";
        postData += "&service=" + "mail";
        postData += "&rm=" + "false";
        postData += "&itmpl=" + "default";
        postData += "&hl=" + "en";
        postData += "&scc=" + "1";
        postData += "ss=" + "1";
        postData += "&sacu=" + "1";
        postData += "&_utf8=" + "☃";
        postData += "&bgresponse=" + "!A0L90r-qE6ZLyUQ7tYD_B09KGgIAAAAjUgAAAAcqARcN5-HZ6OMO9yMKHpX_wJvfu81b67CyiAbn0EbvzuV9yjTly_ZM6err6uAruVCVnx5qUodg7vq6PIaYWpBtVnOzc2Ean-7ITFxt4SqBb0VAKLOJElXwWmximH-oydxBVH05VR4xLmWF1D00_yiPaXcqibx8ihD8yB1e8bOWrmfdzuVgyOfImv1LTSATv-AMI2W0dfTJlWmmngo4yefWiIEAYHJwixpArol4gDtxBAW81W98CMyXPNxXyu3JV3mCUMSrCh1EPJAX3DpfrU7bmD1O-gO7p8Gw5qU1vAuYt61AaLC9ydABYpvd3oysvccseXDdXJEI9fpeoOV8TNc3QvLYarUPbtjG1gYxgMh_zY72ogVucxCoj8U";
        postData += "&pstMsg=" + "1";
        postData += "&dnConn=" + "";
        postData += "&checkConnection=" + "";
        postData += "&checkedDomains=" + "youtube";
        postData += "&Email=" + "myEmailAddress";
        postData += "&Passwd=" + "my password";
        postData += "&signIn=" + "Sign in";
        postData += "&PersistentCookie=" + "yes";
        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
        http.ContentLength = dataBytes.Length;
        using (Stream postStream = http.GetRequestStream())
        {
            postStream.Write(dataBytes, 0, dataBytes.Length);
        }
        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
        // Probably want to inspect the http.Headers here first
        http = WebRequest.Create("https://accounts.google.com/b/0/EditPasswd") as HttpWebRequest;
        http.CookieContainer = new CookieContainer();
        http.CookieContainer.Add(httpResponse.Cookies);

Solution

  • Try setting a cookie container on your request. This answer may help: C# keep session id over httpwebrequest