Example #1
0
        WebClient GetWebClient(IEnumerable <KeyValuePair <string, string> > headers = null, bool isCacheable = false)
        {
            CookieAwareWebClient webClient;

            if (isCacheable)
            {
                webClient = new CookieAwareCacheableWebClient(cookies);
            }
            else
            {
                webClient = new CookieAwareWebClient(cookies);
            }

            webClient.Encoding = Encoding.UTF8;
            if (headers != null)
            {
                foreach (var pairs in headers)
                {
                    webClient.Headers.Add(pairs.Key, pairs.Value);
                }
            }
            if (credentials != null)
            {
                webClient.Credentials = credentials;
            }
            if (connectionInfo.Proxy != null)
            {
                webClient.Proxy = connectionInfo.Proxy;
            }
            return(webClient);
        }
Example #2
0
        public DynamicJsonObject GetCacheable(Uri target, out bool isCachedResult, IDictionary <string, string> headers = null)
        {
            DynamicJsonObject response        = null;
            DateTime          startTime       = DateTime.Now;
            String            requestHeaders  = "";
            String            responseHeaders = "";

            try
            {
                using (var webClient = GetWebClient(headers, true))
                {
                    requestHeaders = webClient.Headers.ToString();
                    CookieAwareCacheableWebClient cacheableWeb = webClient as CookieAwareCacheableWebClient;
                    if (cacheableWeb != null)
                    {
                        response = cacheableWeb.DownloadCacheableResult(target, out isCachedResult);
                    }
                    else
                    {
                        throw new InvalidOperationException("GetWebClient failed to create a CookieAwareCacheableWebClient");
                    }

                    responseHeaders = webClient.ResponseHeaders.ToString();
                    return(response);
                }
            }
            finally
            {
                Trace.TraceInformation("Get ({0}):\r\n{1}\r\nRequest Headers:\r\n{2}Response Headers:\r\n{3}Response Data\r\n{4}",
                                       DateTime.Now.Subtract(startTime).ToString(),
                                       target.ToString(),
                                       requestHeaders,
                                       responseHeaders,
                                       response);
            }
        }