Example #1
0
        //</snippet11>

        //<snippet12>
        public static HttpRequestCachePolicy CreateCacheIfAvailablePolicy()
        {
            HttpRequestCachePolicy policy =
                new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);

            Console.WriteLine(policy.ToString());
            return(policy);
        }
Example #2
0
        //</snippet8>

        //<snippet9>
        public static HttpRequestCachePolicy CreateFreshAndAgePolicy(TimeSpan freshMinimum, TimeSpan ageMaximum)
        {
            HttpRequestCachePolicy policy =
                new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum);

            Console.WriteLine(policy.ToString());
            return(policy);
        }
Example #3
0
        //</snippet7>

        //<snippet8>
        public static HttpRequestCachePolicy CreateDefaultPolicy()
        {
            HttpRequestCachePolicy policy =
                new HttpRequestCachePolicy();

            Console.WriteLine(policy.ToString());
            return(policy);
        }
Example #4
0
 public void Ctor_ExpectedPropertyValues(
     HttpRequestCachePolicy p, HttpRequestCacheLevel level, TimeSpan maxAge, TimeSpan maxStale, TimeSpan minFresh, DateTime cacheSyncDate)
 {
     Assert.Equal(level, p.Level);
     Assert.Equal(maxAge, p.MaxAge);
     Assert.Equal(maxStale, p.MaxStale);
     Assert.Equal(minFresh, p.MinFresh);
     Assert.Equal(cacheSyncDate, p.CacheSyncDate);
     Assert.StartsWith("Level:", p.ToString());
 }
Example #5
0
        //<snippet1>
        public static WebResponse GetResponseUsingDefaultCache(Uri uri)
        {
            // Set a cache policy level for the "http:" scheme.
            HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            // Create the request.
            WebRequest request = WebRequest.Create(uri);

            request.CachePolicy = policy;
            WebResponse response = request.GetResponse();

            Console.WriteLine("Policy {0}.", policy.ToString());
            Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);
            return(response);
        }
Example #6
0
        public string HttpGetFile(string url, string file)
        {
            try
            {
                File.Delete(file);
            }
            catch
            {
            }
            FileStream fs1 = new FileStream(file, FileMode.Create, FileAccess.Write);

            try
            {
                WebRequest request = WebRequest.Create(url);
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                HttpRequestCachePolicy Policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                request.CachePolicy = Policy;

                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                int    Length    = 256;
                Byte[] buffer    = new Byte[Length];
                int    bytesRead = dataStream.Read(buffer, 0, Length);
                // write the required bytes
                while (bytesRead > 0)
                {
                    fs1.Write(buffer, 0, bytesRead);
                    bytesRead = dataStream.Read(buffer, 0, Length);
                }
                dataStream.Close();
                fs1.Close();
                response.Close();
                Console.WriteLine("Policy is {0}.", Policy.ToString());
                Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);
                return("Goed");
            }
            catch (Exception e)
            {
                fs1.Close();
                Bericht(System.Reflection.MethodBase.GetCurrentMethod().Name + " " + e.Message);
                return(e.Message);
            }
        }
Example #7
0
        //</snippet9>

        //<snippet10>
        public static HttpRequestCachePolicy CreateFreshAndAgePolicy2(TimeSpan freshMinimum, TimeSpan ageMaximum, DateTime when)
        {
            HttpRequestCachePolicy policy =
                new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum, when);

            Console.WriteLine(policy.ToString());
            return(policy);
            // For the following invocation:
            // CreateFreshAndAgePolicy(new TimeSpan(5,0,0), new TimeSpan(10,0,0),);
            // the output is:
            // Level:Automatic
            // AgeControl:MinFreshAndMaxAge
            // MinFresh:18000
            // MaxAge:36000
        }