Đăng nhập gmail với c#

Mình có đoạn code bằng c# dùng để đăng nhập vào gmail qua link:
https://accounts.google.com/signin/challenge/sl/password
Phương thức post nhưng không chạy được. Yêu cầu của mình là lấy html trả về sau khi post đăng nhập thành công và lưu lại cookie sau khi đăng nhập.
Các bạn thay account email đăng nhập thử hộ mình nhé.

Bạn nào làm được cho mình xin thông tin liên hệ để cảm ơn nhé.

Mình ở hà nội.

string url = "https://accounts.google.com/signin/challenge/sl/password";



HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.AllowAutoRedirect = false;

Uri uri = new Uri(url);
CookieContainer cook = new CookieContainer();

request.CookieContainer = cook;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

string chitiet = "";
var cookieContainer = new CookieContainer();

using (var httpWebResponse = (HttpWebResponse)request.GetResponse())
{
    using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        foreach (Cookie cookie in httpWebResponse.Cookies)
        {
            cookieContainer.Add(cookie);
            Console.Write(cookie + "\n");
        }
        
    }
}



chitiet = new Program().gethtml("https://accounts.google.com/ServiceLogin");






var doc = new HtmlAgilityPack.HtmlDocument();

 
doc.LoadHtml(chitiet);

var Page = doc.DocumentNode.SelectSingleNode("//input[@name='Page']");
var GALX = doc.DocumentNode.SelectSingleNode("//input[@name='GALX']");
var gxf = doc.DocumentNode.SelectSingleNode("//input[@name='gxf']");
var _utf8 = doc.DocumentNode.SelectSingleNode("//input[@name='_utf8']");
var bgresponse = doc.DocumentNode.SelectSingleNode("//input[@name='bgresponse']");
var pstMsg = doc.DocumentNode.SelectSingleNode("//input[@name='pstMsg']");
var dnConn = doc.DocumentNode.SelectSingleNode("//input[@name='dnConn']");
var checkConnection = doc.DocumentNode.SelectSingleNode("//input[@name='checkConnection']");
var checkedDomains = doc.DocumentNode.SelectSingleNode("//input[@name='checkedDomains']");

var singin = doc.DocumentNode.SelectSingleNode("//input[@id='signIn']");


var request1 = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/signin/challenge/sl/password");

string Email = "demo";
string Passwd = "demo123";



var postData = "Page=" + Page.Attributes["value"].Value;
            postData += "&GALX=" + GALX.Attributes["value"].Value;
postData += "&gxf=" + gxf.Attributes["value"].Value;



postData += "&continue=&service=&hl=";

postData += "&_utf8=" + "☃";
postData += "&bgresponse=" + bgresponse.Attributes["value"].Value;
postData += "&pstMsg=" + pstMsg.Attributes["value"].Value;
postData += "&dnConn=" + dnConn.Attributes["value"].Value;
postData += "&checkConnection=" + checkConnection.Attributes["value"].Value;
postData += "&checkedDomains=" + checkedDomains.Attributes["value"].Value;
postData += "&PersistentCookie=yes";
postData += "&singin=" + singin.Attributes["value"].Value;

postData += "&Email=" + Email;
postData += "&Passwd=" + Passwd;




string getUrl = "https://accounts.google.com/signin/challenge/sl/password";        
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer = cookieContainer;
getRequest.Method = "POST";
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";

getRequest.Referer = "https://accounts.google.com/ServiceLogin";




getRequest.AllowAutoRedirect = false; ;
getRequest.ContentType = "application/x-www-form-urlencoded";

byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();

 

var response1 = (HttpWebResponse)getRequest.GetResponse();

var responseString = new StreamReader(response1.GetResponseStream()).ReadToEnd();
1 Like

Anh ơi anh đã Login Gmail bằng HttpWebRequest được chưa ạ. Em tìm mọi cách chưa ra. Anh làm ơn chỉ em với. Em cám ơn nhiều ạ

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?