【青椒原創】2016C#模擬谷歌Google登陸Gmail&Youtube小案例

来源:http://www.cnblogs.com/x-poior/archive/2016/06/14/5585506.html
-Advertisement-
Play Games

之所以寫這個,是因為本來想寫一個Youtube刷評論的工具,把登錄做出來了,後面就沒繼續做下去。 涉及到基本的HttpWatch的應用以及Fiddler的應用(Fd主要用來排查問題,通過對比 瀏覽器和vs代碼 找出問題所在!以瀏覽器為標準)。 通過HttpWatch抓包分析,我把登錄Youtube一 ...


之所以寫這個,是因為本來想寫一個Youtube刷評論的工具,把登錄做出來了,後面就沒繼續做下去。

涉及到基本的HttpWatch的應用以及Fiddler的應用(Fd主要用來排查問題,通過對比 瀏覽器和vs代碼 找出問題所在!以瀏覽器為標準)。

通過HttpWatch抓包分析,我把登錄Youtube一共分為三個階段:

1. 請求 打開錄入郵箱地址 頁面

2. 請求 回發本郵箱地址,載入郵箱賬號 頭像

3. 請求 回發本郵箱地址和密碼, 登錄Youtube

有很多的Cookie和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 轉載請註明,謝謝!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • linux系統中root用戶可以通過useradd命令添加新用戶,添加的用戶還需要使用passwd設置密碼 ...
  • C#複習① 2016年6月15日 08:19 Main Introduction of C# 簡單介紹C# 1. C# is very similar to Java (70% Java, 10% C++, 5% Visual Basic, 15% new) C#與Java好比失散多年的姐妹,長得像 ...
  • 剛開始學習C#的時候,就聽說CLR對於String類有一種特別的記憶體管理機制:有時候,明明聲明瞭兩個String類的對象,但是他們偏偏卻指向同一個實例。如下: String s1 = "Hello";String s2 = "Hello"; //s2和s1的實際值都是“Hello”bool same ...
  • 說明: 說明一點JS是直接支持Json的 所以可以直接如下寫: var a={"Age":28,"LastLoginTime":"2011-01-09 01:00:56","Name":"張三"}; 直接是json對象 而不必 寫成: var a=’{"Age":28,"LastLoginTime" ...
  • 分類:C#、VS2015 創建日期:2016-06-15 教材:(十二五國家級規劃教材)《C#程式設計及應用教程》(第3版) 一、本章要點 C#數據類型分兩大類:值類型、引用類型。 值類型:棧(Stack)中保存的就是數據的值。 引用類型:棧中保存的是堆(Heap)中對象的引用地址。 棧(Stack... ...
  • 五月中旬 .NET Core RC2 如期發佈,我們遂決定翻譯 ASP.NET Core 文檔。我們在何鎮汐先生、悲夢先生、張仁建先生和雷歐納德先生的群中發佈了翻譯計劃招募信息,並召集到一群小伙伴。我們從六月開始翻譯文檔,目前已完成前兩章,並將按計劃以連載的方式逐篇公佈。當翻譯計劃完成後,所有文檔將... ...
  • 今天來簡單聊聊Suggestion產品 什麼是Suggestion服務? 一圖勝千言: 當你想要搜索某個長詞語或者一句話輸入部分時,Suggestion服務預測你極大可能的候選項,並羅列出來,供你選擇。 產品的意義: 1. 降低用戶搜索的輸入成本,用戶總是懶惰的,誰能讓用戶最懶惰還能幫他把事辦好,這 ...
  • 轉載別人的轉載,原作者都不知道了 在項目中經常用到DataTable,如果DataTable使用得當,不僅能使程式簡潔實用,而且能夠提高性能,達到事半功倍的效果,現對DataTable的使用技巧進行一下總結。 一、DataTable簡介 (1)構造函數 DataTable() 不帶參數初始化Data ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...