Repository navigation
Expand file tree
/
Copy pathAdvanced_007_CustomHeaders.cs
More file actions
48 lines (42 loc) · 1.69 KB
/
Copy pathAdvanced_007_CustomHeaders.cs
File metadata and controls
48 lines (42 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using ClickHouse.Driver.ADO;
using ClickHouse.Driver.Utility;
namespace ClickHouse.Driver.Examples;
/// <summary>
/// Demonstrates how to use custom HTTP headers with ClickHouse.
/// Custom headers can be used for proxy authentication, distributed tracing via correlation ids, etc.
///
/// The Authorization, User-Agent, and Connection headers cannot be overridden.
/// </summary>
public static class CustomHeaders
{
public static async Task Run()
{
Console.WriteLine("Custom HTTP Headers Example\n");
// Add custom headers for proxy authentication
// Useful when connecting through a proxy that requires specific headers
var settings = new ClickHouseClientSettings(ExampleConfig.HttpConnectionString)
{
CustomHeaders = new Dictionary<string, string>
{
["X-Proxy-Authorization"] = "Bearer proxy-token-xyz",
["X-Forwarded-For"] = "192.168.1.100",
["X-Real-IP"] = "192.168.1.100"
}
};
Console.WriteLine(" Settings created with proxy headers:");
Console.WriteLine(" - X-Proxy-Authorization: Bearer proxy-token-xyz");
Console.WriteLine(" - X-Forwarded-For: 192.168.1.100");
Console.WriteLine(" - X-Real-IP: 192.168.1.100");
try
{
using var client = new ClickHouseClient(settings);
var result = await client.ExecuteScalarAsync("SELECT 42");
Console.WriteLine($" Query executed successfully: {result}");
}
catch
{
Console.WriteLine(" Query could not be executed");
}
Console.WriteLine("\nAll custom headers examples completed!");
}
}