How to extract data from a currently open tab in Chrome using C#

Currently, I am working on developing a console application using c# .net. One specific functionality that I require is to extract data from an external website, which involves signing in to access the information. I have managed to open the necessary page with the required credentials using process.start in Chrome; however, I encountered difficulty in retrieving the data from the webpage. I attempted various methods, such as obtaining the source code, but unfortunately, none of them maintained the session, leading to only error page source code being retrieved. Perhaps there could be an alternate approach utilizing JavaScript or c# that would enable successful data extraction?

Answer №1

Utilize the WebClient API to authenticate and retrieve page data. To maintain a session, ensure that it is cookie aware. It is necessary to reference the System.Web Assembly in your console application to use this API.

public class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
    {
        CookieContainer = new CookieContainer();
    }
    public CookieContainer CookieContainer { get; private set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }
}

Now implement it as follows:

using (var client = new CookieAwareWebClient())
{
    var values = new NameValueCollection
    {
        { "username", "jane" },
        { "password", "classified" },
    };
    client.UploadValues("http://domain.loc/login.aspx", values);

    // With a successful authentication, we can now access the protected page
    string result = client.DownloadString("http://domain.loc/dashboard.aspx");
}

This code snippet was sourced from WebClient accessing page with credentials

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Do you have to change the style of each individual element using JavaScript, or is there another method to accomplish this?

Recently, I've been exploring different ways to use JavaScript to globally adjust styles. My goal is to modify the CSS rule that controls the element's style, similar to how you can do it using the Inspector in Webkit. However, after visiting htt ...

Changing the background color of a fixed header in AngularJS based on the current page

I designed my homepage with a full-height div featuring a video. The header is fixed in position and has a transparent background with white text when positioned over the video. As I scroll down, I dynamically adjust the background to white with black text ...

Unforeseen outcomes encountered while invoking a PostgreSQL function with Dapper

I have developed a straightforward postgres function that takes a username and password as input and returns either 1 or 0 based on whether a match is found. I have successfully executed the function manually in pgAdmin, but when I try to call it from my A ...

Developer tools indicate React state property is set despite it always being undefined

Encountering a perplexing problem while using ReactJs along with TyperScript. In the constructor of the component, I initialize the state with a value from the provided props: constructor(props: IEditProps) { super(props); const initialState = { ...

How to stop AngularJS onClick event from causing scroll to jump to top of

As I work on developing a website using Angular JS and Bootstrap, I encountered an issue with the hamburger menu button on my homepage. Whenever I scroll halfway down the page and click the hamburger icon, it automatically scrolls me back to the top of the ...

I was surprised by how Await behaved in if-else statements, it was not what

let metadata = []; allNFTs.map(async (e) => { if (e.metadata) { metadata.push(JSON.parse(e.metadata).attributes); } else { let config = { method: "get", url: `http://localhost:3000/api/fetch ...

Tips for utilizing a variable from a function in one file within a function in another file

Having trouble using the variable counter from one function in a different file? In the first function, I defined counter without using "var" thinking it would make it a global variable. But for some reason, it doesn't seem to work. Help needed! //fun ...

Column alignment issue detected

Can you help me with aligning the data in my column status properly? Whenever I update the data, it doesn't align correctly as shown in the first image. https://i.stack.imgur.com/300Qt.png https://i.stack.imgur.com/4Dcyw.png $('#btn_edit' ...

What action should be taken if there is only one choice in a SELECT statement?

I have created a dynamic form with three interconnected select elements. The first select element is populated with data fetched from a MySQL database using PHP. The second select element is populated based on the selection made in the first one, triggered ...

The jQuery code I'm using is incorrectly selecting all elements with the class name "a2" when I click on them, instead of providing me with

I was hoping to see one a2 element displayed at a time when clicking on a div element, but instead, all a2 elements are appearing simultaneously. I would like to see only one a2 class element at a time upon clicking. <!DOCTYPE html> <html> < ...

What is the process for dynamically looping through a table and adding form data to the table?

I am in the process of developing an hour tracking website that utilizes a form and a table. Currently, I have implemented the functionality where the form content gets added to the table upon the first submission. However, I need it to allow users to inp ...

What are some potential causes of webpack-dev-server's hot reload feature not working properly?

Having an issue with my React project. When I try to use hot reload by running "npm start" or "yarn start" with webpack-dev-server configured (--hot flag), I'm getting the error message: [error message here]. Can anyone assist me in troubleshooting th ...

Is there a way to customize the color of the bar displaying my poll results?

My poll features two results bars that are currently both blue. I attempted to change the color of these bars but was unsuccessful. I've searched for solutions on stack overflow, specifically How can I change the color of a progress bar using javascr ...

Setting up Highcharts version 7 heatmaps with npm in an Angular 6 environment

I am currently having an issue with initializing the heatmap lib in Highcharts 7.0.1 within my Angular 6 app via npm. Despite successfully running basic charts like line, spline, bar, etc., when following the documentation for the heatmap initialization, I ...

Passing data from getServerSideProps to an external component in Next.js using typescript

In my Index.js page, I am using serverSideProps to fetch consumptions data from a mock JSON file and pass it to a component that utilizes DataGrid to display and allow users to modify the values. export const getServerSideProps: GetServerSideProps = async ...

Implementing dynamic loading with a Vue component loader

Is it possible to dynamically import a component using a default Loader if it's not loaded? Currently, we are using the following approach: import Dashboard from '../components/dashboard'; Vue.component('dashboard', Dashboard); ...

Divide the MySQL results into two distinct groups and display them in separate div

I have a MySQL query that fetches all the topic results. I have also implemented a pagination system where the results are divided into pages, and the query's limit #,# varies depending on the current page. My goal is to organize these results into t ...

Develop a back button for PHP data fetched from a database

I have recently developed a quiz exam website where users can answer questions retrieved from a database. The structure of the website includes HTML and AJAX to call PHP functions for handling the questions. Each question is displayed one at a time to the ...

What is the process for retrieving a group of objects from a session in code?

Currently, I am working on creating an invoice system. For registered users, the invoices and their units are saved to the database (where one invoice can have multiple units). On the other hand, for users who are not logged in, I am attempting to store th ...

Is there a way to retrieve the file path of the file that is imported using an alias in Vite (Vue3)?

Hello! I have a few inquiries. Is it feasible to obtain the file path of the file utilizing an alias for import in Vite (Vue3)? Setup Here's the directory structure I'm using, purely for illustrative purposes: src/ module_a/ some_ ...