使用HttpWebRequest登录Gmail,未收到Cookie

时间:2014-12-21 20:24:09

标签: c# .net gmail system.net.httpwebrequest

我想登录我的Gmail帐户并将密码更改100次,以将其设置为我的旧密码。 (正如你所知道谷歌保留了以前100个密码的历史记录,并且不允许你选择其中一个密码)。 我正在使用HttpWebRequest和HttpWebResponse来解决我的问题。我在Fiddler中监控了请求和响应。 但是当我使用以下代码登录gmail时,我没有得到任何cookie。 问题在哪里?!

            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);

1 个答案:

答案 0 :(得分:0)

尝试根据您的请求设置Cookie容器。这个答案可能有所帮助 C# keep session id over httpwebrequest