public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
var origAddress = new Uri(address.Scheme + "://" + address.Authority);
bool usesProxy = false;
bool useConnect = false;
if (proxy != null && !proxy.IsBypassed(address))
{
usesProxy = true;
bool isSecure = address.Scheme == "https";
address = proxy.GetProxy(address);
if (address.Scheme != "http")
{
throw new NotSupportedException("Proxy scheme not supported.");
}
if (isSecure && address.Scheme == "http")
{
useConnect = true;
}
}
address = new Uri(address.Scheme + "://" + address.Authority);
var key = new SPKey(origAddress, usesProxy ? address : null, useConnect);
lock (servicePoints) {
if (servicePoints.TryGetValue(key, out var sp))
{
return(sp);
}
if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
{
throw new InvalidOperationException("maximum number of service points reached");
}
int limit;
#if MOBILE
limit = defaultConnectionLimit;
#else
string addr = address.ToString();
limit = (int)manager.GetMaxConnections(addr);
#endif
sp = new ServicePoint(key, address, limit, maxServicePointIdleTime);
sp.Expect100Continue = expectContinue;
sp.UseNagleAlgorithm = useNagle;
sp.UsesProxy = usesProxy;
sp.UseConnect = useConnect;
sp.SetTcpKeepAlive(tcp_keepalive, tcp_keepalive_time, tcp_keepalive_interval);
return(servicePoints.GetOrAdd(key, sp));
}
}