|
| 1 | +--- |
| 2 | +title: Configure a proxy server when using the Azure SDK for .NET |
| 3 | +description: Learn different ways to configure a proxy server for use with the Azure SDK for .NET client libraries. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.custom: devx-track-dotnet, engagement-fy23 |
| 6 | +ms.date: 11/15/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# Configure a proxy server when using the Azure SDK for .NET |
| 10 | + |
| 11 | +If your organization requires the use of a proxy server to access Internet resources, some configuration is required to use the Azure SDK for .NET client libraries. Once configured, the proxy is applied to the `HttpClient` instance used for HTTP operations. |
| 12 | + |
| 13 | +## Configure using code |
| 14 | + |
| 15 | +To programmatically configure a proxy for use in underlying HTTP operations supporting the Azure SDK for .NET libraries, complete the following steps: |
| 16 | + |
| 17 | +1. Create an <xref:System.Net.Http.HttpClientHandler> object whose `Proxy` property is set. |
| 18 | +1. Create a service client options object whose <xref:Azure.Core.ClientOptions.Transport%2A> property is set to an `HttpClientTransport` object accepting the `HttpClientHandler` instance. |
| 19 | +1. Pass the service client options object to the service client constructor. |
| 20 | + |
| 21 | +Using the Azure Key Vault Secrets library as an example, you'd have the following code: |
| 22 | + |
| 23 | +:::code language="csharp" source="snippets/configure-proxy/Program.cs"::: |
| 24 | + |
| 25 | +## Configure using environment variables |
| 26 | + |
| 27 | +Depending on if your proxy server uses HTTP or HTTPS, you'll set either the environment variable `HTTP_PROXY` or `HTTPS_PROXY`, respectively. The proxy server URL takes the form `http[s]://[username:password@]<ip_address_or_hostname>:<port>/`, where the `username:password` combination is optional. To get the IP address or hostname, port, and credentials for your proxy server, consult your network administrator. |
| 28 | + |
| 29 | +The following examples show how to set the appropriate environment variables in command shell (Windows) and bash (Linux/macOS) environments. Setting the appropriate environment variable causes the Azure SDK for .NET libraries to use the proxy server at runtime. |
| 30 | + |
| 31 | +### [cmd](#tab/cmd) |
| 32 | + |
| 33 | +```cmd |
| 34 | +rem Non-authenticated HTTP server: |
| 35 | +set HTTP_PROXY=http://10.10.1.10:1180 |
| 36 | +
|
| 37 | +rem Authenticated HTTP server: |
| 38 | +set HTTP_PROXY=http://username:[email protected]:1180 |
| 39 | +
|
| 40 | +rem Non-authenticated HTTPS server: |
| 41 | +set HTTPS_PROXY=http://10.10.1.10:1180 |
| 42 | +
|
| 43 | +rem Authenticated HTTPS server: |
| 44 | +set HTTPS_PROXY=http://username:[email protected]:1180 |
| 45 | +``` |
| 46 | + |
| 47 | +### [bash](#tab/bash) |
| 48 | + |
| 49 | +```bash |
| 50 | +# Non-authenticated HTTP server: |
| 51 | +HTTP_PROXY=http://10.10.1.10:1180 |
| 52 | + |
| 53 | +# Authenticated HTTP server: |
| 54 | +HTTP_PROXY=http://username: [email protected]:1180 |
| 55 | + |
| 56 | +# Non-authenticated HTTPS server: |
| 57 | +HTTPS_PROXY=http://10.10.1.10:1180 |
| 58 | + |
| 59 | +# Authenticated HTTPS server: |
| 60 | +HTTPS_PROXY=http://username: [email protected]:1180 |
| 61 | +``` |
| 62 | + |
| 63 | +--- |
0 commit comments