You can configure remote browsers to use your own authenticated proxies. This functionality helps you control the browser’s network connection and IP address, which is useful for tasks like automation, data scraping, and accessing geo-restricted content.
Configuring the Proxy with Authentication
Authenticated proxies must be configured when launching the browser, and authentication is handled by including credentials in the proxy URL.
1. Configure the Proxy with Authentication for the Browser
Here is a C# example showing how to configure and use an authenticated proxy with a browser managed by CloudBrowser.
usingSystem;usingSystem.Net.Http;usingSystem.Threading.Tasks;usingPuppeteerSharp;publicclassProgram{publicstaticasyncTaskMain(string[] args) {var serverUrl ="https://production.cloudbrowser.ai/api/v1/Browser/OpenAdvanced";var apiToken ="your-api-token"; // Replace with your actual API tokenvar requestBody =new { Proxy =new { Host ="my-proxy.com", Port ="8080", Username ="user", Password ="password" } };var jsonContent =newStringContent(JsonConvert.SerializeObject(requestBody),Encoding.UTF8,"application/json"); // Create an HttpClient and include the API token in the request headersvar httpClient =newHttpClient();httpClient.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Bearer", apiToken); // Request the WebSocket endpoint of the remote browservar response =awaithttpClient.PostAsync(serverUrl, jsonContent); // Get address valuevar responseContent =awaitresponse.Content.ReadAsStringAsync();dynamic jsonResponse =JsonConvert.DeserializeObject(responseContent);var browserWSEndpoint =jsonResponse.address; // Connect to the remote browservar browser =awaitPuppeteer.ConnectAsync(newConnectOptions { BrowserWSEndpoint = browserWSEndpoint, }); // Create a new pagevar page =awaitbrowser.NewPageAsync(); // Navigate to a URL to verify the IPawaitpage.GoToAsync("https://ifconfig.me"); // Close the browserawaitbrowser.CloseAsync(); }}
Additional Considerations
URL Formatting: Ensure the proxy URL is correctly formatted to include authentication credentials.
Error Handling: Implement error handling to capture and manage possible issues when connecting to the proxy or remote browser.