2016C#模拟谷歌(谷歌)Google登陆Gmail&Youtube小案例

之所以写那么些,是因为自然想写一个Youtube刷评论的工具,把登录做出来了,前面就没继续做下来。

波及到要旨的Http沃特ch的施用以及Fiddler的施用(Fd主要用于排查难点,通过对照
浏览器和vs代码 找出难点所在!以浏览器为专业)。

经过Http沃特ch抓包分析,我把登录Youtube一共分为多个阶段:

  1. 请求 打开录入邮箱地址 页面

  2. 恳请 回发本邮箱地址,加载邮箱账号 头像

  3. 伸手 回发本邮箱地址和密码, 登录Youtube

有过多的库克ie和PostData的话是 服务器不做校验的,也就是说主要的
Cookie和Post/Get数据并不是全方位您所看到的,这些你可往日期模拟登陆成功之后,逐个删掉测试看是否仍是可以登陆成功 来判断 该参数是还是不是必须!

上面的原理其实在做任何 网站的效仿 登录 中也是千篇一律的。

大家平素看代码吧。

 1 private void button1_Click(object sender, EventArgs e)
 2         { 
 3             var cookieJar = new CookieContainer();
 4             CookieAwareWebClient webClient = new CookieAwareWebClient(cookieJar);
 5 
 6             string GAPS = string.Empty;
 7             string GALX = string.Empty;
 8             string GoogleAccountsLocale_session = string.Empty;
 9 
10             string NID = string.Empty;
11             string ProfileInformation = string.Empty; 
12 
13             //1.第一部分init
14             string url1 = "https://accounts.google.com/ServiceLogin?sacu=1&continue=https%3A%2F%2F"
15             +"www.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26feature%3Dsign_in_button%26next%3D%252Fwatch%253Fv%253DpMUv7qmKQik%26hl%3Dzh-CN&hl=zh-CN&service=youtube";//地址  
16 
17 
18             string srcString1 = webClient.DownloadString(url1);//解码  
19 
20             //receive cookie 
21              GAPS = GetCookie("GAPS", cookieJar);
22              GALX = GetCookie("GALX", cookieJar);
23              GoogleAccountsLocale_session = GetCookie("GoogleAccountsLocale_session", cookieJar);
24              
25 
26             //2.第二部分input email
27             webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 
28             string postString2 = "_utf8=☃" + "&bgresponse="
29                 + "&checkConnection=youtube:724:0" + "&checkedDomains=youtube" + "&continue=https://www.youtube.com/signin?action_handle_signin=true&app=desktop&feature=sign_in_button&next=%2Fwatch%3Fv%3DpMUv7qmKQik&hl=zh-CN"
30                 + "&dnConn=" + "&Email=【此处填写自己的Gmail邮箱】" + "&GALX=" + GALX + "&gxf=AFoagUVXYmSP1FoIo4SFJlAauKdrDab_0A:1465396649942"
31                 + "&hl=zh-CN" + "&Page=PasswordSeparationSignIn" + "&ProfileInformation=" + "&pstMsg=1" + "&sacu=1"
32                 + "&service=youtube" + "&signIn=下一步";
33 
34 
35             byte[] postData2 = Encoding.UTF8.GetBytes(postString2);//编码,尤其是汉字,事先要看下抓取网页的编码方式  
36             string url2 = "https://accounts.google.com/AccountLoginInfo";//地址  
37 
38             webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可  
39             byte[] responseData2 = webClient.UploadData(url2, "POST", postData2);//得到返回字符流  
40             string srcString2 = Encoding.UTF8.GetString(responseData2);//解码  
41 
42             GAPS = GetCookie("GAPS", cookieJar);
43 
44            //本文地址:http://www.cnblogs.com/x-poior/p/5585506.html 转载请注明,谢谢!
45 
46             //3.第三部分input pw 
47            //   webClient.Headers.Add("Referer", "https://accounts.google.com/AccountLoginInfo");
48            //   ProfileInformation = QuMiddle(srcString2, @"name=""ProfileInformation"" type=""hidden"" value=""", @""">"); 后面排除法得出结论,该参数不是必需的,
49 
50             string postString3 = "_utf8=☃" + "&bgresponse="
51                 + "&checkConnection=youtube:724:0" + "&checkConnection=youtube:398:0"
52                 + "&checkedDomains=youtube" + "&checkedDomains=youtube"
53                 + "&continue=https://www.youtube.com/signin?action_handle_signin=true&app=desktop&feature=sign_in_button&next=%2Fwatch%3Fv%3DpMUv7qmKQik&hl=zh-CN"
54                 + "&dnConn=" + "&Email=【此处填写自己的Gmail邮箱】" + "&GALX=" + GALX + "&gxf=AFoagUVXYmSP1FoIo4SFJlAauKdrDab_0A:1465396649942"
55                 + "&hl=zh-CN" + "&Page=PasswordSeparationSignIn" + "&Passwd=【此处填写自己的Gmail密码】"
56                 + "&PersistentCookie=yes" + "&ProfileInformation=" + "&pstMsg=1" + "&pstMsg=1"
57                 + "&rmShown=1" + "&sacu=1"
58                 + "&service=youtube" + "&signIn=登录";
59 
60              
61             byte[] postData3 = Encoding.UTF8.GetBytes(postString3);//编码,尤其是汉字,事先要看下抓取网页的编码方式  
62             string url3 = "https://accounts.google.com/ServiceLoginAuth";//地址  
63               
64             webClient.Method = "POST";
65             string srcString3 = webClient.UploadString(url3, postString3);
66 
67 
68             //第四部分,校验Cookie数目判断是否成功!
69             var lastCookie = cookieJar;//注意,如果第三部分,返回的Response的Cookie的数量有>20 个的话,说明登陆成功了!Google 返回给你很多凭证,可以登录Google大部分的产品、
70             var s2s4 = srcString3;
71 
72 
73             var agereg = webClient.DownloadString("https://www.youtube.com/watch?v=VYwkmp97-B4");//左边是受年龄限制的视频,不登陆验证是没法观看下载的!!返回Html-String里含有“ytplayer.config”说明可以观看下载!
74 
75              
76         }

 

任何的多少个协助方法,帮助类。

/// <summary>
        /// 获取Cookie的值
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <param name="cc">Cookie集合对象</param>
        /// <returns>返回Cookie名称对应值</returns>
        public static string GetCookie(string cookieName, CookieContainer cc)
        {
            List<Cookie> lstCookies = new List<Cookie>();
            Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
                System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
            foreach (object pathList in table.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
                    | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                    foreach (Cookie c1 in colCookies) lstCookies.Add(c1);
            }
            var model = lstCookies.Find(p => p.Name == cookieName);
            if (model != null)
            {
                return model.Value;
            }
            return string.Empty;
        }

     //本文地址:http://www.cnblogs.com/x-poior/p/5585506.html 转载请注明,谢谢!

/// <summary> /// 取字符串中间 /// </summary> /// <param name="str">总字符串</param> /// <param name="str1">左边字符串</param> /// <param name="str2">右边字符串</param> /// <returns>中间字符串</returns> public static string QuMiddle(string str, string str1, string str2) { int leftlocation;//左边位置 int rightlocation;//右边位置  int strmidlength; ;//中间字符串长度 string strmid;//中间字符串  leftlocation = str.IndexOf(str1); if (leftlocation == -1) { return ""; } leftlocation = leftlocation + str1.Length;//获取左边字符串尾所在位置  rightlocation = str.IndexOf(str2, leftlocation);//获取右边字符串头所在位置  if (rightlocation == -1 || leftlocation > rightlocation)//判断右边字符串是否存在于总字符串中,左边字符串位置是否在右边字符串前  { return ""; } strmidlength = rightlocation - leftlocation;//计算中间字符串长度  strmid = str.Substring(leftlocation, strmidlength);//取出中间字符串  return strmid;//返回中间字符串  }

CookieAwareWebClient .cs

 

 1     public class CookieAwareWebClient : WebClient
 2     {
 3         public string Method;
 4         public CookieContainer CookieContainer { get; set; }
 5         public Uri Uri { get; set; }
 6 
 7         public CookieAwareWebClient()
 8             : this(new CookieContainer())
 9         {
10         }
11 
12         public CookieAwareWebClient(CookieContainer cookies)
13         {
14             this.CookieContainer = cookies;
15             this.Encoding = Encoding.UTF8;
16         }
17 
18         protected override WebRequest GetWebRequest(Uri address)
19         {
20             WebRequest request = base.GetWebRequest(address);
21             if (request is HttpWebRequest)
22             {
23                 (request as HttpWebRequest).CookieContainer = this.CookieContainer;
24                 (request as HttpWebRequest).ServicePoint.Expect100Continue = false;
25                 (request as HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.5 Safari/537.36";
26                 (request as HttpWebRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
27                 (request as HttpWebRequest).Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8,en;q=0.6,nl;q=0.4,zh-TW;q=0.2");
28                 (request as HttpWebRequest).Referer = "";
29                 (request as HttpWebRequest).KeepAlive = true;
30                 (request as HttpWebRequest).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
31                 if (Method == "POST")
32                 {
33                     (request as HttpWebRequest).ContentType = "application/x-www-form-urlencoded";
34                 }
35             }
36             HttpWebRequest httpRequest = (HttpWebRequest)request;
37             httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
38             return httpRequest;
39         }
40 
41         protected override WebResponse GetWebResponse(WebRequest request)
42         {
43             WebResponse response = base.GetWebResponse(request);
44             String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
45 
46             if (setCookieHeader != null)
47             {
48                 //do something if needed to parse out the cookie.
49                 try
50                 {
51                     if (setCookieHeader != null)
52                     {
53                         Cookie cookie = new Cookie();
54                         cookie.Domain = request.RequestUri.Host;
55                         this.CookieContainer.Add(cookie);
56                     }
57                 }
58                 catch (Exception)
59                 {
60 
61                 }
62             }
63             return response;
64         }
65     }

 本文地址:http://www.cnblogs.com/x-poior/p/5585506.html
转发请评释,谢谢!

相关文章