Generate HAR (HTTP Archive) file from chrome browser

120 Views Asked by At

I'm new to selenium, I want to generate HAR (HTTP Archive) file from chrome browser, Please help me to figure out how can I achieve this using c#. Thanks in advance.

 public void StartBrowserMobProxy()
        {
            // batch file path (Release + BrowserMobProcess + browsermob-proxy.bat)
            string pathToProxyExecutable =  Application.StartupPath + "\\BrowserMobProcess\\" + "browsermob-proxy.bat"; 
            string proxyPort = "8080"; // Choose a port number for the proxy

            browserMobProcess = new Process();
            browserMobProcess.StartInfo.FileName = pathToProxyExecutable;
            browserMobProcess.StartInfo.Arguments = "--port " + proxyPort;
            browserMobProcess.Start();
        }

        /// <summary>
        /// Need to Stop the BrowserMob Proxy Server after finish
        /// </summary>
        public void StopBrowserMobProxy()
        {
            if (browserMobProcess != null && !browserMobProcess.HasExited)
            {
                browserMobProcess.Kill();
                browserMobProcess.Dispose();
            }
        }
private void CaptureHAR()
        {
            string proxyUrl = "http://localhost:8080"; // Use the same port you used when starting the proxy
            _httpClient = new HttpClient(new HttpClientHandler { Proxy = new WebProxy(proxyUrl) });

            // Perform a request through the proxy to start capturing HAR
            HttpResponseMessage response = _httpClient.GetAsync(this._url).Result;

            // You can save the HAR data from the response if needed
            string harJson = response.Content.ReadAsStringAsync().Result;
            //  HAR data append in a file


        }

0

There are 0 best solutions below