Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
6.6 kB
1
Indexable
Never
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using System;
using System.Threading;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using OpenQA.Selenium.DevTools;

using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V95.DevToolsSessionDomains;
using Network = OpenQA.Selenium.DevTools.V95.Network;
using OpenQA.Selenium.DevTools.V95.Network;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace ConsoleApp3
{
    internal class Program
    {
        static protected IDevToolsSession session;
        static protected IWebDriver driver;
        static protected DevToolsSessionDomains devToolsSession;
        static Task<GetResponseBodyCommandResponse> body;
        static IDevTools devTools;
        static string id;
        static async Task Main(string[] args)
        {
            new DriverManager().SetUpDriver(new ChromeConfig());
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("--headless");
            //Set ChromeDriver
            driver = new ChromeDriver();
            //Get DevTools
             devTools = driver as IDevTools;
            //DevTools Session
            session = devTools.GetDevToolsSession();

            devToolsSession = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
            devToolsSession.Network.Enable(new Network.EnableCommandSettings());

            //devToolsSession.Network.ResponseReceived += ResponseReceivedHandlerAsync;
            
            devToolsSession.Network.ResponseReceived += async (sender, e) =>
            {
                Console.WriteLine($"**Url: { e.Response.Url } Status: { e.Response.Status } Type: { e.Response.MimeType } ReqId : {e.RequestId}");
                //Console.WriteLine("*********TYPE" + e.GetType().ToString());
                Headers headers = e.Response.Headers;
                Boolean do_check = false;
                foreach (var h in headers)
                {
                    //Console.WriteLine(h.Key + "------" + h.Value);
                    if(h.Key == ":path" && h.Value== "/fc/ekey/")
                    {
                        do_check = true;
                    }
                }

                if (do_check)
                {
                    string i = e.RequestId;

                    // Network  Enable
                    await devToolsSession.Network.Enable(new EnableCommandSettings());

                    GetResponseBodyCommandSettings cmd = new GetResponseBodyCommandSettings();
                    cmd.RequestId = i;
                    if (i != null && i.Length > 0)
                    {
                        Console.WriteLine("I != null ");
                        try
                        {
                            var waiter = devToolsSession.Network.GetResponseBody(cmd).GetAwaiter();
                            if (waiter.GetResult() != null)
                            {
                                string[] lines;
                                Console.WriteLine("Waiter completed ");
                                //Console.WriteLine("BODY___" + waiter.GetResult().Body.ToString());
                                if (waiter.GetResult().Base64Encoded == true)
                                {
                                    byte[] data = Convert.FromBase64String(waiter.GetResult().Body);
                                    string decodedString = Encoding.UTF8.GetString(data);
                                    lines = new string[] { decodedString };
                                }
                                else
                                {
                                    lines = new string[] { waiter.GetResult().Body };
                                }


                                //await File.WriteAllLinesAsync( (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) + "WriteLines.txt", lines);

                                await File.WriteAllLinesAsync((waiter.GetResult().Base64Encoded == true ? "base" : "nobase") + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) + "WriteLines.txt", lines);
                            }
                        }
                        catch (Exception ex)
                        {

                        }


                    }
                }
                


            };

            driver.Navigate().GoToUrl("https://www.ea.com/it-it/fifa/ultimate-team/web-app/");

            while (true)
            {
                Thread.Sleep(1000);
            }
            
            
            //GetResponseBodyCommandSettings settings = new GetResponseBodyCommandSettings();
            //settings.RequestId = id;
            //devToolsSession.Network.GetResponseBody(settings).Start();

        }


        public static void ResponseReceivedHandlerAsync(object sender, ResponseReceivedEventArgs e)
        {
            
            Console.WriteLine($"**Url: { e.Response.Url } Status: { e.Response.Status } Type: { e.Response.MimeType } ReqId : {e.RequestId}");
            Console.WriteLine("*********TYPE"+e.GetType().ToString());
            Headers headers = e.Response.Headers;
            
            foreach( var h in headers)
            {
                Console.WriteLine(h.Key + "------" + h.Value);
            }

            string re = e.RequestId;
            
            //Console.WriteLine(e.Response.StatusText.ToString());
            //id = e.RequestId;

            //Console.WriteLine("Finish"+b.ToString());


        }


        /*
        static async Task Main(string[] args)
        {

            var driver = new ChromeDriver();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            var devTools = (IDevTools)driver;
            DevToolsSession session = devTools.GetDevToolsSession();
            // add response received handler to dev tools session
            session.Network.ResponseReceived += ResponseReceivedHandler;
            await session.Network.Enable(new EnableCommandSettings());
            driver.Navigate().GoToUrl("https://stackoverflow.com/");
            Thread.Sleep(5000);
        }

        public static void ResponseReceivedHandler(object sender, ResponseReceivedEventArgs e)
        {
            Console.WriteLine($"Url: { e.Response.Url } Status: { e.Response.Status } Type: { e.Response.MimeType } ");
        }
        */
    }
}