Setting User-Agent
Introduction
Configuring a Custom User-Agent
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 custom User-Agent
var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"; // Replace with your desired User-Agent
// 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 the custom User-Agent
await page.SetUserAgentAsync(userAgent);
// Navigate to a URL to verify the User-Agent
await page.GoToAsync("https://www.whatismybrowser.com/");
// Close the browser
await browser.CloseAsync();
}
}Additional Considerations
Last updated