Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Determine whether you are behind a proxy or not with .NET

You can get the default proxy setting for a specific url with System.Net.WebRequest.GetSystemWebProxy().GetProxy().

The result is System.Uri object, and its OriginalString property will be the same the parameter if you are not behind a proxy:

PS C:\> [System.Net.WebRequest]::GetSystemWebProxy().GetProxy('https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize')


AbsolutePath   : /organizations/oauth2/v2.0/authorize
AbsoluteUri    : https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize
LocalPath      : /organizations/oauth2/v2.0/authorize
Authority      : login.microsoftonline.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /organizations/oauth2/v2.0/authorize
Segments       : {/, organizations/, oauth2/, v2.0/...}
IsUnc          : False
Host           : login.microsoftonline.com
Port           : 443
Query          :
Fragment       :
Scheme         : https
OriginalString : https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize
DnsSafeHost    : login.microsoftonline.com
IdnHost        : login.microsoftonline.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

The OriginalString property will be the same the proxy URL if if you are behind a proxy:

PS C:\> [System.Net.WebRequest]::GetSystemWebProxy().GetProxy('https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize')


AbsolutePath   : /
AbsoluteUri    : http://140.227.25.56:5678/
LocalPath      : /
Authority      : 140.227.25.56:5678
HostNameType   : IPv4
IsDefaultPort  : False
IsFile         : False
IsLoopback     : False
PathAndQuery   : /
Segments       : {/}
IsUnc          : False
Host           : 140.227.25.56
Port           : 5678
Query          :
Fragment       :
Scheme         : http
OriginalString : http://140.227.25.56:5678
DnsSafeHost    : 140.227.25.56
IdnHost        : 140.227.25.56
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :