private Stream GetNonFileStream( Uri uri, ICredentials credentials ) {
            WebRequest req = WebRequest.Create( uri );
            if ( credentials != null ) {
                req.Credentials = credentials;
            }
            WebResponse resp = req.GetResponse();
            HttpWebRequest webReq = req as HttpWebRequest;
            if ( webReq != null ) {
                lock ( this ) {
                    if ( connections == null ) {
                        connections = new Hashtable();
                    }
                    OpenedHost openedHost = (OpenedHost)connections[webReq.Address.Host];
                    if ( openedHost == null ) {
                        openedHost = new OpenedHost();
                    }

                    if ( openedHost.nonCachedConnectionsCount < webReq.ServicePoint.ConnectionLimit - 1 ) {
                        // we are not close to connection limit -> don't cache the stream
                        if ( openedHost.nonCachedConnectionsCount == 0 ) {
                            connections.Add( webReq.Address.Host, openedHost );
                        }
                        openedHost.nonCachedConnectionsCount++;
                        return new XmlRegisteredNonCachedStream( resp.GetResponseStream(), this, webReq.Address.Host );
                    }
                    else {
                        // cache the stream and save the connection for the next request
                        return new XmlCachedStream( resp.ResponseUri, resp.GetResponseStream() );
                    }
                }
            }
            else {
                return resp.GetResponseStream();
            }
        }
Exemplo n.º 2
0
        private async Task <Stream> GetNonFileStreamAsync(Uri uri, ICredentials credentials, IWebProxy proxy,
                                                          RequestCachePolicy cachePolicy)
        {
            WebRequest req = WebRequest.Create(uri);

            if (credentials != null)
            {
                req.Credentials = credentials;
            }
            if (proxy != null)
            {
                req.Proxy = proxy;
            }
            if (cachePolicy != null)
            {
                req.CachePolicy = cachePolicy;
            }

            WebResponse resp = await Task <WebResponse> .Factory.FromAsync(req.BeginGetResponse, req.EndGetResponse, null).ConfigureAwait(false);

            HttpWebRequest webReq = req as HttpWebRequest;

            if (webReq != null)
            {
                lock (this) {
                    if (connections == null)
                    {
                        connections = new Hashtable();
                    }
                    OpenedHost openedHost = (OpenedHost)connections[webReq.Address.Host];
                    if (openedHost == null)
                    {
                        openedHost = new OpenedHost();
                    }

                    if (openedHost.nonCachedConnectionsCount < webReq.ServicePoint.ConnectionLimit - 1)
                    {
                        // we are not close to connection limit -> don't cache the stream
                        if (openedHost.nonCachedConnectionsCount == 0)
                        {
                            connections.Add(webReq.Address.Host, openedHost);
                        }
                        openedHost.nonCachedConnectionsCount++;
                        return(new XmlRegisteredNonCachedStream(resp.GetResponseStream(), this, webReq.Address.Host));
                    }
                    else
                    {
                        // cache the stream and save the connection for the next request
                        return(new XmlCachedStream(resp.ResponseUri, resp.GetResponseStream()));
                    }
                }
            }
            else
            {
                return(resp.GetResponseStream());
            }
        }
 internal void Remove(string host)
 {
     lock (this)
     {
         OpenedHost host2 = (OpenedHost)this.connections[host];
         if ((host2 != null) && (--host2.nonCachedConnectionsCount == 0))
         {
             this.connections.Remove(host);
         }
     }
 }
 internal void Remove(string host)
 {
     lock (this) {
         OpenedHost openedHost = (OpenedHost)connections[host];
         if (openedHost != null)
         {
             if (--openedHost.nonCachedConnectionsCount == 0)
             {
                 connections.Remove(host);
             }
         }
     }
 }
        private Stream GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
        {
            WebRequest request = WebRequest.Create(uri);

            if (credentials != null)
            {
                request.Credentials = credentials;
            }
            if (proxy != null)
            {
                request.Proxy = proxy;
            }
            if (cachePolicy != null)
            {
                request.CachePolicy = cachePolicy;
            }
            WebResponse    response = request.GetResponse();
            HttpWebRequest request2 = request as HttpWebRequest;

            if (request2 != null)
            {
                lock (this)
                {
                    if (this.connections == null)
                    {
                        this.connections = new Hashtable();
                    }
                    OpenedHost host = (OpenedHost)this.connections[request2.Address.Host];
                    if (host == null)
                    {
                        host = new OpenedHost();
                    }
                    if (host.nonCachedConnectionsCount < (request2.ServicePoint.ConnectionLimit - 1))
                    {
                        if (host.nonCachedConnectionsCount == 0)
                        {
                            this.connections.Add(request2.Address.Host, host);
                        }
                        host.nonCachedConnectionsCount++;
                        return(new XmlRegisteredNonCachedStream(response.GetResponseStream(), this, request2.Address.Host));
                    }
                    return(new XmlCachedStream(response.ResponseUri, response.GetResponseStream()));
                }
            }
            return(response.GetResponseStream());
        }
        private Stream GetNonFileStream(Uri uri, ICredentials credentials)
        {
            WebRequest req = WebRequest.Create(uri);

            if (credentials != null)
            {
                req.Credentials = credentials;
            }
            WebResponse    resp   = req.GetResponse();
            HttpWebRequest webReq = req as HttpWebRequest;

            if (webReq != null)
            {
                lock (this) {
                    if (connections == null)
                    {
                        connections = new Hashtable();
                    }
                    OpenedHost openedHost = (OpenedHost)connections[webReq.Address.Host];
                    if (openedHost == null)
                    {
                        openedHost = new OpenedHost();
                    }

                    if (openedHost.nonCachedConnectionsCount < webReq.ServicePoint.ConnectionLimit - 1)
                    {
                        // we are not close to connection limit -> don't cache the stream
                        if (openedHost.nonCachedConnectionsCount == 0)
                        {
                            connections.Add(webReq.Address.Host, openedHost);
                        }
                        openedHost.nonCachedConnectionsCount++;
                        return(new XmlRegisteredNonCachedStream(resp.GetResponseStream(), this, webReq.Address.Host));
                    }
                    else
                    {
                        // cache the stream and save the connection for the next request
                        return(new XmlCachedStream(resp.ResponseUri, resp.GetResponseStream()));
                    }
                }
            }
            else
            {
                return(resp.GetResponseStream());
            }
        }
 private Stream GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
 {
     WebRequest request = WebRequest.Create(uri);
     if (credentials != null)
     {
         request.Credentials = credentials;
     }
     if (proxy != null)
     {
         request.Proxy = proxy;
     }
     if (cachePolicy != null)
     {
         request.CachePolicy = cachePolicy;
     }
     WebResponse response = request.GetResponse();
     HttpWebRequest request2 = request as HttpWebRequest;
     if (request2 != null)
     {
         lock (this)
         {
             if (this.connections == null)
             {
                 this.connections = new Hashtable();
             }
             OpenedHost host = (OpenedHost) this.connections[request2.Address.Host];
             if (host == null)
             {
                 host = new OpenedHost();
             }
             if (host.nonCachedConnectionsCount < (request2.ServicePoint.ConnectionLimit - 1))
             {
                 if (host.nonCachedConnectionsCount == 0)
                 {
                     this.connections.Add(request2.Address.Host, host);
                 }
                 host.nonCachedConnectionsCount++;
                 return new XmlRegisteredNonCachedStream(response.GetResponseStream(), this, request2.Address.Host);
             }
             return new XmlCachedStream(response.ResponseUri, response.GetResponseStream());
         }
     }
     return response.GetResponseStream();
 }
        private async Task<Stream> GetNonFileStreamAsync(Uri uri, ICredentials credentials, IWebProxy proxy,
            RequestCachePolicy cachePolicy) {
            WebRequest req = WebRequest.Create(uri);
            if (credentials != null)    {
                req.Credentials = credentials;
            }
            if (proxy != null)  {
                req.Proxy = proxy;
            }
            if (cachePolicy != null)    {
                req.CachePolicy = cachePolicy;
            }

            WebResponse resp = await Task<WebResponse>.Factory.FromAsync(req.BeginGetResponse, req.EndGetResponse, null).ConfigureAwait(false);
            HttpWebRequest webReq = req as HttpWebRequest;
            if (webReq != null) {
                lock (this) {
                    if (connections == null)    {
                        connections = new Hashtable();
                    }
                    OpenedHost openedHost = (OpenedHost)connections[webReq.Address.Host];
                    if (openedHost == null) {
                        openedHost = new OpenedHost();
                    }

                    if (openedHost.nonCachedConnectionsCount < webReq.ServicePoint.ConnectionLimit - 1) {
                        // we are not close to connection limit -> don't cache the stream
                        if (openedHost.nonCachedConnectionsCount == 0)  {
                            connections.Add(webReq.Address.Host, openedHost);
                        }
                        openedHost.nonCachedConnectionsCount++;
                        return new XmlRegisteredNonCachedStream(resp.GetResponseStream(), this, webReq.Address.Host);
                    }
                    else {
                        // cache the stream and save the connection for the next request
                        return new XmlCachedStream(resp.ResponseUri, resp.GetResponseStream());
                    }
                }
            }
            else {
                return resp.GetResponseStream();
            }
        }