# Setting User-Agent

### Introduction <a href="#id-0-toc-title" id="id-0-toc-title"></a>

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 <a href="#id-1-toc-title" id="id-1-toc-title"></a>

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**.

<pre class="language-csharp"><code class="lang-csharp">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

<strong>        // Create an HttpClient and include the API token in the request headers
</strong>        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();
    }
}
</code></pre>

**2. Code Explanation**

1. **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
```

2. **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);
```

3. **Connect to the Remote Browser**: Use `Puppeteer.ConnectAsync` to connect to the browser.

```
   var browser = await Puppeteer.ConnectAsync(new ConnectOptions
   {
       BrowserWSEndpoint = browserWSEndpoint
   });
```

4. **Create a New Page**: Create a new page instance in the browser.

```
   var page = await browser.NewPageAsync();
```

5. **Set the Custom User-Agent**: Use `page.SetUserAgentAsync` to apply the custom User-Agent string to the page.

```
   await page.SetUserAgentAsync(userAgent);
```

6. **Navigate and Verify**: Navigate to a site like `https://www.whatismybrowser.com/` to verify the User-Agent.

```
   await page.GoToAsync("https://www.whatismybrowser.com/");
```

7. **Close the Browser**: Close the browser when finished.

```
   await browser.CloseAsync();
```

#### Additional Considerations <a href="#id-4-toc-title" id="id-4-toc-title"></a>

* **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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cloudbrowser.gitbook.io/docs/configurations/setting-user-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
