public CartWebClient(CartWebClient source)
     : base()
 {
     CookieContainer = new CookieContainer();
     //_requestTimeout = source._requestTimeout;
     //_sessionTimeout = source._sessionTimeout;
     //_bufferSize = source._bufferSize;
     _lastuse = source._lastuse;
     _config = source._config;
     QueryString = new NameValueCollection(source.QueryString);
     Headers = new WebHeaderCollection();
     Headers.Add(source.Headers);
 }
        protected void SetupFeedClient(string feedUrl, string authHeader = null, NameValueCollection queryParams = null, string acceptHeader = null)
        {
            const string DefaultAcceptHeader = "application/json";
            var cancelAuth = false;

            if (string.IsNullOrEmpty(acceptHeader))
                acceptHeader = string.IsNullOrEmpty(Rules.ApiAcceptHeader) ? DefaultAcceptHeader : Rules.ApiAcceptHeader;

            if (_feedClient != null)
            {
                // remove old query strings and replace them below
                _feedClient.QueryString.Clear();

                //if the existing web client is using an auth header, but the new request is not, then we need to kill it
                cancelAuth = !string.IsNullOrEmpty(_feedClient.Headers["Authorization"])
                                 && string.IsNullOrEmpty(authHeader);
            }

            if (_feedClient == null || _feedClient.SessionExpired() || cancelAuth)
            {
                //start over with a new client
                _feedClient = new CartWebClient(Rules.WebClientConfig)
                    {
                        Encoding = Encoding.UTF8,
                        BaseAddress = "",
                        Proxy = null
                    };

                var login = GetLogin();
                if (login != null)
                {
                    _feedClient.Headers.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
                    _feedClient.Headers.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    //_feedClient.Headers.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
                    //_feedClient.Headers.Set("accept", "*/*");
                    //_feedClient.Headers.Set("content-type", "application/x-www-form-urlencoded");
                    var loginUrl = string.IsNullOrEmpty(Rules.ExtractorLoginUrl) ? feedUrl : Rules.ExtractorLoginUrl;
                    //var loginPage = _feedClient.DownloadString(loginUrl);
                    var responseArray = _feedClient.UploadValues(loginUrl, "POST", login);
            #if DEBUG
                    var loginResponse = Encoding.UTF8.GetString(responseArray);
                    Debug.Write(loginResponse);
            #endif
                }
                else if (Rules.ExtractorCredentials != null && Rules.ExtractorCredentials.Type == AuthCredentials.AuthType.HttpAuth)
                {
                    _feedClient.Credentials = new NetworkCredential(Rules.ExtractorCredentials.UserName, Rules.ExtractorCredentials.Password);
                    ServicePointManager.ServerCertificateValidationCallback =
                        ((sender, certificate, chain, sslPolicyErrors) => true);
                }
            }

            //must set headers each time as previous call may remove them
            _feedClient.Headers.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
            _feedClient.Headers.Set("accept", acceptHeader);
            if (!string.IsNullOrEmpty(authHeader))
                _feedClient.Headers.Set("Authorization", authHeader);
            if (queryParams != null && queryParams.Count > 0)
                _feedClient.QueryString.Add(queryParams);

            #if DEBUG
            if (BoostLog.Instance != null)
                BoostLog.Instance.WriteEntry(EventLogEntryType.Information, GetRequestDetails(feedUrl));
            #endif
        }
        protected string GetRequestDetails(string feedUrl)
        {
            if (feedUrl.StartsWith("file:", StringComparison.OrdinalIgnoreCase)) return feedUrl;

            var details = string.Format("Feed Request Details\nURL = {0}\nQuery Params:\n", feedUrl);
            var fullUrl = feedUrl;
            if (_feedClient == null)
                return details + "\tnone\n";
            try
            {
                var tempClient = new CartWebClient(_feedClient); //it's possible for _feedClient to be nulled in a separate thread
                if (tempClient.QueryString.HasKeys())
                {
                    var separator = "?";
                    foreach (string k in tempClient.QueryString.Keys)
                    {
                        var v = tempClient.QueryString.GetValues(k);
                        var vals = v == null ? "" : v.Any() ? v.Aggregate((c, w) => string.Format("{0},{1}", c, w)) : "";
                        details += string.Format("\t{0}:\t{1}\n", k, vals);
                        fullUrl += separator + k + "=" + vals;
                        separator = "&";
                    }
                }
                else
                    details += "\tnone\n";

                details += "Headers:\n";
                if (tempClient.Headers.HasKeys())
                {
                    foreach (string k in tempClient.Headers.Keys)
                    {
                        var v = tempClient.Headers.GetValues(k);
                        details += string.Format("\t{0}:\t{1}\n", k, v == null
                                                                         ? ""
                                                                         : v.Any()
                                                                               ? v.Aggregate((c, w) => string.Format("{0},{1}", c, w))
                                                                               : "");
                    }
                }
                else
                    details += "\tnone\n";
            }
            catch (Exception ex)
            {
                details += string.Format("\nError in GetRequestDetails: {0}", Input.GetExMessage(ex));
            }
            details += "\nFull Url: " + fullUrl;

            return details;
        }