[edit: this top post has been edited to show the final draft proposal for a new API]
API Proposal
namespace System.Net.Http
{
public class HttpClient
{
...
public static IWebProxy DefaultProxy { get; set; }
...
}
}
Behaviors
The property will default to a non-null value representing the platform/system proxy. With PR 37328, all platforms can use environment variables as the first possible choice of proxy settings. If those variables are set, then an IWebProxy interface of an instance of the internal HttpEnvironmentProxy class is returned.
If the environment variables are not set, then the following happens for the default value of this property:
- On Windows, the IWebProxy interface of the internal HttpSystemProxy class is returned. HttpSystemProxy wraps the IE/Wininet settings.
- On OSX, the IWebProxy interface of the internal MacProxy class is returned. MacProxy wraps the OSX system proxy configuration.
- On Linux, if environment variables are not set, then a non-null instance of an IWebProxy object is returned. It will return 'true' for IsBypassed() for any Uri passed in.
The property will never return null.
The property can be set to any object implementing the IWebProxy interface. This property cannot be set to 'null'. Doing so will throw an exception, ArgumentNullException.
Sample API usage
// I want to pass in default credentials to the system proxy so my any HttpClient request
// will work on my corporate network.
// This includes any HttpClient objects that I might not create as long as they use the
// default settings of HttpClientHandler.
HttpClient.DefaultProxy.Credentials = CredentialCache.DefaultCredentials;
// I want to use a special proxy everywhere I use HttpClient
HttpClient.DefaultProxy = new MyCustomProxy();
History of proxy support in .NET
https://gist.github.com/davidsh/f8768c02faf714de9a3029cbf0166f18
Background
HttpClient has support for reading system proxy configuration (IE settings on Windows, Environment variables on Linux, etc). However, there is no mechanism for setting credentials (i.e. CredentialCache.DefaultCredentials) for a system configured proxy. Currently, this requires creating an HttpClientHandler instance and setting its DefaultProxyCredentials property.
For cases where libraries are creating the HttpClient object, there is no way for consumers of the library to set these credentials easily.
The .NET Framework supports using app.config/web.config settings with a system.net section:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
This allows applications to inject a global proxy configuration for any HttpClient that is created. However, .NET Core doesn't natively support configuration files.
.NET Framework currently has the WebRequest.DefaultWebProxy property to control this for all HttpWebRequest objects (and affects HttpClient) as well. On .NET Core, the WebRequest class is considered legacy. But we need a similar way to set global proxy config on .NET Core for HttpClient usage.
A static API on HttpClient (or other object) created for global proxy configuration for the application could be used by upstack components to create a similar configuration file (i.e. app.config) behavior.
[edit: this top post has been edited to show the final draft proposal for a new API]
API Proposal
Behaviors
The property will default to a non-null value representing the platform/system proxy. With PR 37328, all platforms can use environment variables as the first possible choice of proxy settings. If those variables are set, then an IWebProxy interface of an instance of the internal HttpEnvironmentProxy class is returned.
If the environment variables are not set, then the following happens for the default value of this property:
The property will never return null.
The property can be set to any object implementing the IWebProxy interface. This property cannot be set to 'null'. Doing so will throw an exception,
ArgumentNullException.Sample API usage
History of proxy support in .NET
https://gist.github.com/davidsh/f8768c02faf714de9a3029cbf0166f18
Background
HttpClient has support for reading system proxy configuration (IE settings on Windows, Environment variables on Linux, etc). However, there is no mechanism for setting credentials (i.e. CredentialCache.DefaultCredentials) for a system configured proxy. Currently, this requires creating an HttpClientHandler instance and setting its DefaultProxyCredentials property.
For cases where libraries are creating the HttpClient object, there is no way for consumers of the library to set these credentials easily.
The .NET Framework supports using app.config/web.config settings with a system.net section:
This allows applications to inject a global proxy configuration for any HttpClient that is created. However, .NET Core doesn't natively support configuration files.
.NET Framework currently has the WebRequest.DefaultWebProxy property to control this for all HttpWebRequest objects (and affects HttpClient) as well. On .NET Core, the WebRequest class is considered legacy. But we need a similar way to set global proxy config on .NET Core for HttpClient usage.
A static API on HttpClient (or other object) created for global proxy configuration for the application could be used by upstack components to create a similar configuration file (i.e. app.config) behavior.