diff --git a/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs b/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs index 42faa0a78d7233..7e1b3fbe01f1a2 100644 --- a/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs +++ b/src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs @@ -35,7 +35,7 @@ public WebProxy(Uri? Address, bool BypassOnLocal, string[]? BypassList, ICredent } public WebProxy(string Host, int Port) - : this(new Uri(string.Create(CultureInfo.InvariantCulture, $"http://{Host}:{Port}")), false, null, null) + : this(CreateProxyUri(Host, Port), false, null, null) { } @@ -102,10 +102,27 @@ public bool UseDefaultCredentials return IsBypassed(destination) ? destination : Address; } - private static Uri? CreateProxyUri(string? address) => - address == null ? null : - !address.Contains("://") ? new Uri("http://" + address) : - new Uri(address); + private static Uri? CreateProxyUri(string? address, int? port = null) + { + if (address is null) + { + return null; + } + + if (!address.Contains("://", StringComparison.Ordinal)) + { + address = "http://" + address; + } + + var proxyUri = new Uri(address); + + if (port.HasValue && proxyUri.IsAbsoluteUri) + { + proxyUri = new UriBuilder(proxyUri) { Port = port.Value }.Uri; + } + + return proxyUri; + } private void UpdateRegexList(bool canThrow) { diff --git a/src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs b/src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs index 48e4174aea5172..cfb2cd10e947a0 100644 --- a/src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs +++ b/src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs @@ -16,7 +16,12 @@ public static IEnumerable Ctor_ExpectedPropertyValues_MemberData() yield return new object[] { new WebProxy(), null, false, false, Array.Empty(), null }; yield return new object[] { new WebProxy("http://anything"), new Uri("http://anything"), false, false, Array.Empty(), null }; + yield return new object[] { new WebProxy("http://anything:42"), new Uri("http://anything:42"), false, false, Array.Empty(), null }; + yield return new object[] { new WebProxy("anything:42"), new Uri("http://anything:42"), false, false, Array.Empty(), null }; yield return new object[] { new WebProxy("anything", 42), new Uri("http://anything:42"), false, false, Array.Empty(), null }; + yield return new object[] { new WebProxy("http://anything", 42), new Uri("http://anything:42"), false, false, Array.Empty(), null }; + yield return new object[] { new WebProxy("http://anything:123", 42), new Uri("http://anything:42"), false, false, Array.Empty(), null }; + yield return new object[] { new WebProxy("socks5://anything", 42), new Uri("socks5://anything:42"), false, false, Array.Empty(), null }; yield return new object[] { new WebProxy(new Uri("http://anything")), new Uri("http://anything"), false, false, Array.Empty(), null }; yield return new object[] { new WebProxy("http://anything", true), new Uri("http://anything"), false, true, Array.Empty(), null };