Setting cookies
Introduction
Configuring Cookies
using System;
using System.Net.Http;
using System.Threading.Tasks;
using PuppeteerSharp;
public class Program
{
public static async Task Main(string[] args)
{
var serverUrl = "https://production.cloudbrowser.ai/api/v1/Browser/Open";
var apiToken = "your-api-token"; // Replace with your actual API token
// Define the cookies
var cookies = new CookieParam[]
{
new CookieParam { Name = "example_cookie", Value = "cookie_value", Domain = "example.com" },
new CookieParam { Name = "session_id", Value = "123456", Domain = "example.com" }
};
// Create an HttpClient and include the API token in the request headers
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
// Request the WebSocket endpoint of the remote browser
var browserWSEndpoint = await httpClient.GetStringAsync(serverUrl);
// Connect to the remote browser
var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
BrowserWSEndpoint = browserWSEndpoint
});
// Create a new page
var page = await browser.NewPageAsync();
// Set cookies
await page.SetCookieAsync(cookies);
// Navigate to a URL to verify the cookies
await page.GoToAsync("https://www.cloudbrowser.ai");
// Close the browser
await browser.CloseAsync();
}
}Additional Considerations
Last updated