吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8304|回复: 10
上一主题 下一主题
收起左侧

[其他转载] 关于获取cookie方法的实验(以抖音pc端为例)

   关闭 [复制链接]
跳转到指定楼层
楼主
bjszz 发表于 2022-7-18 10:43 回帖奖励
我将利用三种方法来实验获取cookie,分别是HttpWebRequest,HttpWebResponse,提交请求的方式获取。CefSharp方式和Selenium方式 获取代码在下方,一、HttpWebRequest,HttpWebResponse,提交请求的方式获取,手机扫码登录pc端获取cookie.
1、获取二维码链接:
[C#] 纯文本查看 复制代码
                string url = "https://sso.douyin.com/get_qrcode/?aid=6383&app_name=douyin_web&device_platform=web&referer=&user_agent=Mozilla%2F5.0+(Windows+NT+6.3%3B+WOW64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F55.0.2883.87+UBrowser%2F6.2.4098.3+Safari%2F537.36&cookie_enabled=true&screen_width=1600&screen_height=900&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+6.3%3B+WOW64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F55.0.2883.87+UBrowser%2F6.2.4098.3+Safari%2F537.36&browser_online=true&timezone_name=Asia%2FShanghai&next=https:%2F%2Fcreator.douyin.com%2F&service=https:%2F%2Fwww.douyin.com&is_vcd=1&fp=kesopiod_fB4eRss7_Stsz_434j_AiPQ_6xhtUsqmqpSN";

2、请求二维码代码:
[Asm] 纯文本查看 复制代码
 public static string GetUrl(string Url)
        {
            string html = "";
            string charset = "utf-8";
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  //创建一个链接
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
                request.Referer = Url;
                request.Proxy = null;
                request.Headers.Add("Accept-Encoding", "gzip");
                request.KeepAlive = true;
                request.Accept = "*/*";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;  //获取反馈
                if (response.Headers["Content-Encoding"] == "gzip")
                {
                    GZipStream gzip = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);//解压缩
                    StreamReader reader = new StreamReader(gzip, Encoding.GetEncoding(charset));
                    html = reader.ReadToEnd();
                    reader.Close();
                }
                else
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(charset));
                    html = reader.ReadToEnd();
                    reader.Close();
                }

                response.Close();
                return html;



            }
            catch (System.Exception ex)
            {
                return ex.ToString();

            }



        }


3、请求返回Base64转图片方法:
[C#] 纯文本查看 复制代码
public Image Base64ToImage(string strbase64)
        {
            try
            {
                byte[] arr = Convert.FromBase64String(strbase64);
                MemoryStream ms = new MemoryStream(arr);
                System.Drawing.Image mImage = System.Drawing.Image.FromStream(ms);
                ms.Close();
                return mImage;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }

4、获取二维码成功后,可用手机扫码,建立时间事件来监听扫码行为,处理扫码成功后方法:
[C#] 纯文本查看 复制代码
public void getSetCookie()
        {
            string Url = "https://sso.douyin.com/check_qrconnect/?aid=6383&app_name=douyin_web&device_platform=web&referer=&user_agent=Mozilla%2F5.0+(Windows+NT+6.3%3B+WOW64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F55.0.2883.87+UBrowser%2F6.2.4098.3+Safari%2F537.36&cookie_enabled=true&screen_width=1600&screen_height=900&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+6.3%3B+WOW64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F55.0.2883.87+UBrowser%2F6.2.4098.3+Safari%2F537.36&browser_online=true&timezone_name=Asia%2FShanghai&next=https:%2F%2Fcreator.douyin.com%2F&token=" + token + "&service=https:%2F%2Fwww.douyin.com%2F&correct_service=https:%2F%2Fwww.douyin.com%2F&is_vcd=1&fp=kesopiod_fB4eRss7_Stsz_434j_AiPQ_6xhtUsqmqpSN";
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  //创建一个链接
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
            request.Referer = Url;

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;  //获取反馈
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string html = reader.ReadToEnd();
            string status = Regex.Match(html, @"""status"":""([\s\S]*?)""").Groups[1].Value;
            string redirect_url = Regex.Match(html, @"""redirect_url"":""([\s\S]*?)""").Groups[1].Value;
            if (status == "1")
            {
                label1.Text = "未扫码..";
            }
            else if (status == "2")
            {
                label1.Text = "已扫码..";
            }
            else if (redirect_url != "")
            {
                timer1.Stop();
                label1.Text = "登录成功";
                string content = response.GetResponseHeader("Set-Cookie");
                Uri uri = new Uri("http://douyin.com");
                string cookies = CookieHelper.GetCookies(content, uri);
                string cookies2 = getSetCookie2(cookies);
                textBox1.Text = cookies2 + cookies;
                //insertcookie(cookies2 + cookies);

            }
            reader.Close();


        }


5、处理扫码成功后方法2:
[C#] 纯文本查看 复制代码
public string getSetCookie2(string cookie)
        {
            string Url = "https://sso.douyin.com/check_login/?service=https:%2F%2Fwww.douyin.com&aid=6383";
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  //创建一个链接
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
            request.Referer = Url;
            request.Headers.Add("Cookie", cookie);
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;  //获取反馈
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string html = reader.ReadToEnd();
            string has_login = Regex.Match(html, @"""has_login"":([\s\S]*?),").Groups[1].Value;

            if (has_login == "true")
            {
                reader.Close();
                label1.Text = "登录成功";
                string content = response.GetResponseHeader("Set-Cookie");
                Uri uri = new Uri("http://douyin.com");
                string cookies = CookieHelper.GetCookies(content, uri);
                return cookies;
            }

            else
            {
                reader.Close();
                label1.Text = "登录失败";
                return "";

            }



        }

6、扫码后提示“请使用手机验证码登录”,实验结果获取cookie失败。只是提示已扫码。
二、利用CefSharp获取抖音cookie,并保存本地。
1、添加控件tabPage1。添加窗体载入事件,加载抖音网页,代码:
[C#] 纯文本查看 复制代码
 private void Webbrowser_Load(object sender, EventArgs e)
        {
            browser = new ChromiumWebBrowser(get_code_url);
            Control.CheckForIllegalCrossThreadCalls = false;
            tabPage1.Controls.Add(browser);
           
        }

2、通过网页扫码提示登录成功,添加获取cookie按钮,调用获取cookie方法,并保存本地:
[C#] 纯文本查看 复制代码
 private void button5_Click(object sender, EventArgs e)
        {
            CookieVisitor visitor = new CookieVisitor();
            visitor.SendCookie += Visitor_SendCookie;
            ICookieManager cookieManager = browser.GetCookieManager();
            cookieManager.VisitAllCookies(visitor);
            MessageBox.Show("cookie已获取");
            //dlchangee();
            //this.Close();
        }
        public static string COOKIE = "";
        private void Visitor_SendCookie(CefSharp.Cookie obj)
        {
            //obj.Domain.TrimStart('.') + "^" +
            COOKIE += obj.Name + "=" + obj.Value + ";";
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"cookie.txt", COOKIE, Encoding.Default);
            textBox2.Text = COOKIE;
            //string[] cookies1 = webbrowser.cookies.Split(';');
        }

        public class CookieVisitor : CefSharp.ICookieVisitor
        {
            public event Action<CefSharp.Cookie> SendCookie;
            public void Dispose()
            {

            }

            public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
            {
                deleteCookie = false;
                SendCookie?.Invoke(cookie);

                return true;

            }

           
        }


3、成功获取到cookie,但是由于CefSharp包太大,软件打包后不方便,不推荐此方法。
三、利用Selenium包获取cookie,
1、下载谷歌浏览器的chromedriver.exe放入软件根目录,在谷歌浏览器界面打开抖音主页扫码,
利用Selenium的IWebDriver自带获取cookie方法webDriver.Manage().Cookies.AllCookies;获取cookie,但是发生异常,异常提示
cookie的name为空,可利用webDriver.Manage().Cookies.DeleteCookieNamed("");去除cookie内空name,然后调用webDriver.Manage().Cookies.AllCookies;方法,以上消失。
[C#] 纯文本查看 复制代码
public void login()
        {
       
            
            webDriver.Navigate().GoToUrl(get_code_url);
            Thread.Sleep(10000);
            ReadOnlyCollection<Cookie> cookies1;
            while (true)
            {
                if (webDriver.PageSource.Contains("退出登录"))
                {
                webDriver.Manage().Cookies.DeleteCookieNamed("");
                cookies1 = webDriver.Manage().Cookies.AllCookies;
                break;
                }
              
               
            }
           
            
            StringBuilder sb = new StringBuilder();
            foreach (var item in cookies1)
            {
                sb.Append(item.Name + "=" + item.Value + ";");

            }
            textBox1.Text = sb.ToString();
          
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"logincookie.txt", textBox1.Text, Encoding.Default); 
           
           

        }

2、然后利用获取的cookie,加载到浏览器就可以登录。
[C#] 纯文本查看 复制代码
 webDriver.Navigate().GoToUrl(get_code_url);
            Thread.Sleep(10000);
           
            if (File.Exists(path))
            {
                
                string cookies2 = File.ReadAllText(path);
                string[] cookieal3 = cookies2.Split(new string[] { ";" }, StringSplitOptions.None);

                foreach (var item in cookieal3)
                {
                    if (!string.IsNullOrEmpty(item) && !item.Equals("="))
                    {
                        string[] cookie = item.Split(new string[] { "=" }, StringSplitOptions.None);
                        if (cookie.Length > 1)
                        {
                            OpenQA.Selenium.Cookie cook = new OpenQA.Selenium.Cookie(cookie[0].Trim(), cookie[1].Trim(), "", DateTime.Now.AddDays(1));
                            webDriver.Manage().Cookies.AddCookie(cook);
                        }
                    }

                }


            }
            else {

                MessageBox.Show("请先登录");
                return;
            }

总结,本次实验,利用了三种方法,对抖音cookie进行了获取,第一种方法是利用HttpWebRequest,HttpWebResponse,提交请求的方式获取,由于方式特殊,被拦截,第二种方法获取也很方便,但nuget包巨大,不推荐,第三种方法,利用谷歌浏览器获取,对不熟悉操作的人也不是很方便,后两种方法,都获取成功。

免费评分

参与人数 6吾爱币 +11 热心值 +5 收起 理由
ayang0513 + 1 + 1 热心回复!
晚辈小生 + 1 + 1 用心讨论,共获提升!
coolcat + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
mfxsh + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
sanprolove + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
Lsy123456 发表于 2022-7-18 10:58
看起来有点复杂。学习一下
3#
sanprolove 发表于 2022-7-18 11:01
4#
tudous 发表于 2022-7-18 11:32
5#
tencentma 发表于 2022-7-18 11:59
最近刚好也在学习获取cookie方法,有用!
6#
福森108 发表于 2022-7-18 14:26
厉害,谢谢分享
7#
Whiblackte 发表于 2022-7-18 17:42
学习一下,感谢分享
8#
等待机会 发表于 2022-7-20 21:15
方法1系统繁忙扫码不行。
9#
 楼主| bjszz 发表于 2022-7-21 09:01 |楼主
等待机会 发表于 2022-7-20 21:15
方法1系统繁忙扫码不行。

方法1通过接口 直接扫的 绕不过去的
10#
geiwopo 发表于 2022-12-6 22:53
还是没学会
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-21 23:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表