You can set a custom User-Agent string for remote browsers. This allows you to emulate different browsers, devices, or operating systems, which is useful for testing, web scraping, or accessing content optimized for specific platforms.
Configuring a Custom User-Agent
The User-Agent must be set when launching the browser, affecting all pages opened by that browser instance. Below are the steps to configure a custom User-Agent.
1. Configure the Custom User-Agent for the Browser
Here is a C# example showing how to configure and use a custom User-Agent with a browser managed by CloudBrowser.
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();
}
}
2. Code Explanation
Define the Custom User-Agent: Set the userAgent variable with the desired User-Agent string.
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
Connect to the Server: Get the WebSocket endpoint for the browser from the CloudBrowser API.
var httpClient = new HttpClient();
var browserWSEndpoint = await httpClient.GetStringAsync(serverUrl);
Connect to the Remote Browser: Use Puppeteer.ConnectAsync to connect to the browser.