public HttpRequestCachePolicy(HttpRequestCacheLevel level) : base(MapLevel(level))
 {
     this.m_LastSyncDateUtc = DateTime.MinValue;
     this.m_MaxAge = TimeSpan.MaxValue;
     this.m_MinFresh = TimeSpan.MinValue;
     this.m_MaxStale = TimeSpan.MinValue;
     this.m_Level = level;
 }
Example #2
0
 public HttpRequestCachePolicy(HttpRequestCacheLevel level) : base(MapLevel(level))
 {
     this.m_LastSyncDateUtc = DateTime.MinValue;
     this.m_MaxAge          = TimeSpan.MaxValue;
     this.m_MinFresh        = TimeSpan.MinValue;
     this.m_MaxStale        = TimeSpan.MinValue;
     this.m_Level           = level;
 }
 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 #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
        static public Bitmap BitmapFromUrl(String playerImgUrl)
        {
            Bitmap bmp = null;
            HttpRequestCacheLevel  cacheLevel  = HttpRequestCacheLevel.CacheIfAvailable; // CacheIfAvailable, Reload
            HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy(cacheLevel);
            HttpWebRequest         request;
            HttpWebResponse        response;
            Stream receiveStream = null;

            try
            {
                request             = (HttpWebRequest)WebRequest.Create("http://chips3.nt4.com/image_proxy.php/" + EncodeTo64(playerImgUrl));
                request.CachePolicy = cachePolicy;
                request.ServicePoint.ConnectionLimit = 1;
                request.KeepAlive = true;
                request.Proxy     = new WebProxy();
                response          = (HttpWebResponse)request.GetResponse();
                receiveStream     = response.GetResponseStream();
                if (receiveStream != null)
                {
                    try
                    {
                        bmp = new Bitmap(receiveStream);
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine("Caught exception processing downloaded image: {0}", ex.Message);
                        bmp = null;
                        throw new Exception(ex.Message);
                    }
                }

                Console.WriteLine("Before reading stream: used " + (response.IsFromCache ? "CACHE" : "SERVER")
                                  + " version for servicing request "
                                  + playerImgUrl + " (cache level = " + cacheLevel + ")" + Environment.NewLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught exception downloading {0}: {1}", playerImgUrl, ex.Message);
            }

            if (receiveStream != null)
            {
                receiveStream.Close();
            }

            return(bmp);
        }
Example #6
0
 private static RequestCacheLevel MapLevel(HttpRequestCacheLevel level)
 {
     if (level <= HttpRequestCacheLevel.NoCacheNoStore)
     {
         return((RequestCacheLevel)level);
     }
     if (level == HttpRequestCacheLevel.CacheOrNextCacheOnly)
     {
         return(RequestCacheLevel.CacheOnly);
     }
     if (level != HttpRequestCacheLevel.Refresh)
     {
         throw new ArgumentOutOfRangeException("level");
     }
     return(RequestCacheLevel.Reload);
 }
 private static RequestCacheLevel MapLevel(HttpRequestCacheLevel level)
 {
     if (level <= HttpRequestCacheLevel.NoCacheNoStore)
     {
         return (RequestCacheLevel) level;
     }
     if (level == HttpRequestCacheLevel.CacheOrNextCacheOnly)
     {
         return RequestCacheLevel.CacheOnly;
     }
     if (level != HttpRequestCacheLevel.Refresh)
     {
         throw new ArgumentOutOfRangeException("level");
     }
     return RequestCacheLevel.Reload;
 }
Example #8
0
        /// <summary>
        /// ConvertFrom - attempt to convert to a RequestCachePolicy from the given object
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if the example object is null or is not a valid type
        /// which can be converted to a RequestCachePolicy.
        /// </exception>
        public override object ConvertFrom(ITypeDescriptorContext td, System.Globalization.CultureInfo ci, object value)
        {
            if (null == value)
            {
                throw GetConvertFromException(value);
            }

            string s = value as string;

            if (null == s)
            {
                throw new ArgumentException(SR.Get(SRID.General_BadType, "ConvertFrom"), "value");
            }

            HttpRequestCacheLevel level = (HttpRequestCacheLevel)Enum.Parse(typeof(HttpRequestCacheLevel), s, true);

            return(new HttpRequestCachePolicy(level));
        }
Example #9
0
 /// <summary>
 /// 配置缓存策略
 /// </summary>
 /// <param name="cacheLevel">缓存级别</param>
 public TRequest ConfigCachePolicy(HttpRequestCacheLevel cacheLevel)
 {
     CachePolicy = new HttpRequestCachePolicy(cacheLevel);
     return(This());
 }
 /// <summary>
 /// Initialize a new Http request with the specified cache level.
 /// </summary>
 /// <param name="requestUrl">Url for resource to request.</param>
 /// <param name="cacheLevel">Cache level for request.</param>
 public PluginHttpRequest(string requestUrl, HttpRequestCacheLevel cacheLevel)
 {
     _requestUrl = requestUrl;
     _cacheLevel = cacheLevel;
 }
Example #11
0
        static public void WebReport(Exception ex)
        {
            HttpRequestCacheLevel  cacheLevel  = HttpRequestCacheLevel.BypassCache; // CacheIfAvailable, Reload
            HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy(cacheLevel);
            WebRequest             request;
            WebResponse            response;

            try
            {
                request             = (HttpWebRequest)WebRequest.Create(url); //  + EncodeTo64(playerImgUrl));
                request.CachePolicy = cachePolicy;


                // http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
                // Create a request using a URL that can receive a post.
                // WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.

                JavaScriptSerializer        oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                Dictionary <String, String> jsonError   = new Dictionary <string, string>();
                jsonError.Add("Message", ex.Message);
                jsonError.Add("StackTrace", ex.StackTrace);
                if (ex.Data.Count > 0)
                {
                    foreach (System.Collections.DictionaryEntry pair in ex.Data)
                    {
                        if (pair.Key is string && pair.Value is string)
                        {
                            if (pair.Value.ToString().StartsWith("file://"))
                            {
                                // using (BinaryReader b = new BinaryReader(File.Open((pair.Value as string).Replace("file://", ""), FileMode.Open, FileAccess.Read))) {
                                jsonError.Add(pair.Key.ToString(), FileToBase64((pair.Value as string).Replace("file://", "")));
                            }
                            else
                            {
                                jsonError.Add(pair.Key.ToString(), pair.Value.ToString());
                            }
                        }
                    }
                }
                string postData  = oSerializer.Serialize(jsonError);
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/json";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                // dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                // StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                // string responseFromServer = reader.ReadToEnd();
                // Display the content.
                // Console.WriteLine(responseFromServer);
                // Clean up the streams.
                // reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message + ex.StackTrace);
            }
        }
        protected HttpWebRequest CreateWebRequest( Uri uri, HttpRequestCacheLevel cacheLevel = DefaultRequestCacheLevel )
        {
            HttpWebRequest result = WebRequest.Create( uri ) as HttpWebRequest;

             if( result != null )
             {
            HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy( cacheLevel );
            result.CachePolicy = cachePolicy;
             }

             return result;
        }
Example #13
0
 public HttpRequestCachePolicy(HttpRequestCacheLevel level)
 {
     throw new NotImplementedException();
 }
Example #14
0
        public static string GetData(Uri resourceLocation, NameValueCollection headers = null, HttpRequestCacheLevel cachingLevel = HttpRequestCacheLevel.Default)
        {
            var http = (HttpWebRequest)WebRequest.Create(resourceLocation);

            http.CachePolicy = new HttpRequestCachePolicy(cachingLevel);
            http.Method      = WebRequestMethods.Http.Get;

            ApplyHeaders(http, headers);

            using (WebResponse response = http.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return(string.Empty);
                    }

                    return(new StreamReader(stream).ReadToEnd());
                }
            }
        }
Example #15
0
 //
 public HttpRequestCachePolicy(HttpRequestCacheLevel level): base(MapLevel(level))
 {
     m_Level = level;
 }
		public HttpRequestCachePolicy (HttpRequestCacheLevel level)
		{
			this.level = level;
		}
 public HttpRequestCachePolicy(HttpRequestCacheLevel level)
 {
 }
Example #18
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Net.Cache.HttpRequestCachePolicy" /> class using the specified cache policy.</summary>
 /// <param name="level">An <see cref="T:System.Net.Cache.HttpRequestCacheLevel" /> value. </param>
 public HttpRequestCachePolicy(HttpRequestCacheLevel level)
 {
     this.level = level;
 }
 //
 public HttpRequestCachePolicy(HttpRequestCacheLevel level) : base(MapLevel(level))
 {
     m_Level = level;
 }
 /// <summary>
 /// Initialize a new Http request with the specified cache level.
 /// </summary>
 /// <param name="requestUrl">Url for resource to request.</param>
 /// <param name="cacheLevel">Cache level for request.</param>
 public PluginHttpRequest(string requestUrl, HttpRequestCacheLevel cacheLevel)
 {
     _requestUrl = requestUrl;
     _cacheLevel = cacheLevel;
 }
 public HttpRequestCachePolicy(HttpRequestCacheLevel level)
 {
 }