Setting User-Agent
Introduction
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.
var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
BrowserWSEndpoint = browserWSEndpoint
});
Create a New Page: Create a new page instance in the browser.
var page = await browser.NewPageAsync();
Set the Custom User-Agent: Use
page.SetUserAgentAsync
to apply the custom User-Agent string to the page.
await page.SetUserAgentAsync(userAgent);
Navigate and Verify: Navigate to a site like
https://www.whatismybrowser.com/
to verify the User-Agent.
await page.GoToAsync("https://www.whatismybrowser.com/");
Close the Browser: Close the browser when finished.
await browser.CloseAsync();
Additional Considerations
User-Agent Formatting: Ensure the User-Agent string is correctly formatted to reflect the desired browser and device characteristics.
Compatibility: Verify that the User-Agent string does not conflict with other browser settings or functionalities.
Last updated