private void UpdateConfigFiles(WebClient client, ref bool bCancelled) { ReportProgress(1, 0, "Updating configuration files..."); RequestCacheLevel cacheLevel = _verboseMode ? RequestCacheLevel.Revalidate : RequestCacheLevel.Default; IRemoteContentSaver[] savers = new IRemoteContentSaver[4]; savers[0] = new RemoteFileSaver(_worker, client, _cdnSubdomain + "data/ss_general_data.dat", _generalDataPath, cacheLevel, new TempToPermFileMover()); savers[1] = new RemoteFileSaver(_worker, client, _cdnSubdomain + "data/ss_adcond_data.dat", _advertDataPath, cacheLevel, new TempToPermFileMover()); savers[2] = new RemoteDynamicContentSaver(_worker, client, _mDataSubdomain + "subscriberinfo/demographicdata/" + _userGUID, _demographicDataPath, _password, cacheLevel, new TempToPermFileMover()); savers[3] = new RemoteDynamicContentSaver(_worker, client, _mDataSubdomain + "subscriberinfo/subscriptions/" + _machineGUID, _userChannelSubscriptionsPath, _password, cacheLevel, new TempToPermFileMover()); int localStep = 25; int overallStep = 2; foreach (IRemoteContentSaver saver in savers) { saver.SaveFromRemote(); ReportProgress(localStep, overallStep); if (saver.CancelRequested) { bCancelled = true; return; } localStep += 25; overallStep += 3; } }
public void SendGetRequest_WithGlobalCachePolicy_AddCacheHeaders( RequestCacheLevel requestCacheLevel, string[] expectedHeaders) { RemoteExecutor.Invoke(async(reqCacheLevel, eh0, eh1) => { await LoopbackServer.CreateServerAsync(async(server, uri) => { WebRequest.DefaultCachePolicy = new RequestCachePolicy(Enum.Parse <RequestCacheLevel>(reqCacheLevel)); WebRequest request = WebRequest.Create(uri); Task <WebResponse> getResponse = request.GetResponseAsync(); await server.AcceptConnectionAsync(async connection => { List <string> headers = await connection.ReadRequestHeaderAndSendResponseAsync(); Assert.Contains(eh0, headers); Assert.Contains(eh1, headers); }); using (var response = (HttpWebResponse)await getResponse) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }); }, requestCacheLevel.ToString(), expectedHeaders[0], expectedHeaders[1]).Dispose(); }
public RequestCachePolicy(RequestCacheLevel level) { if (level < RequestCacheLevel.Default || level > RequestCacheLevel.NoCacheNoStore) throw new ArgumentOutOfRangeException("level"); m_Level = level; }
public RequestCachePolicy(RequestCacheLevel level) { if ((level < RequestCacheLevel.Default) || (level > RequestCacheLevel.NoCacheNoStore)) { throw new ArgumentOutOfRangeException("level"); } this.m_Level = level; }
internal void StartDownload(RequestCacheLevel cacheLevel, ICredentials credentials, Func <Uri, string, Dictionary <string, string>, NetworkCredential, ICredentials> httpUnauthorizedHandler) { this.requestCacheLevel = cacheLevel; this.requestCredentials = credentials; this.httpAuthenticationHandler = httpUnauthorizedHandler; this.StartDownload(); }
public RequestCachePolicy(RequestCacheLevel level) { if (level < RequestCacheLevel.Default || level > RequestCacheLevel.NoCacheNoStore) { throw new ArgumentOutOfRangeException(nameof(level)); } Level = level; }
public RemoteFileSaver(BackgroundWorker worker, WebClient client, string url, string localPath, RequestCacheLevel cacheLevel, ITempToPermFileMover mover) { _worker = worker; _client = client; _url = url; _localPath = localPath; _cacheLevel = cacheLevel; _mover = mover; }
public RemoteDynamicContentSaver(BackgroundWorker worker, WebClient client, string url, string localPath, string encryptionPassword, RequestCacheLevel cacheLevel, ITempToPermFileMover mover) { _worker = worker; _client = client; _url = url; _localPath = localPath; _encryptionPassword = encryptionPassword; _cacheLevel = cacheLevel; _mover = mover; }
private bool SetRemoteFileCachePolicy(string value, string setter) { try { this.RemoteFileCachePolicy = (RequestCacheLevel)Enum.Parse(typeof(RequestCacheLevel), value); Console.WriteLine(setter + ": Setting '" + REMOTE_CACHE_POLICY_COMMAND + "' to '" + value + "'."); return(true); } catch { } string[] expected = Enum.GetNames(typeof(RequestCacheLevel)); Console.WriteLine(setter + ": Ignoring parameter '" + REMOTE_CACHE_POLICY_COMMAND + "'. Reason: unrecognized value '" + value + "'" + Environment.NewLine + "\tExpected one of the following: " + string.Join(", ", expected)); return(false); }
private bool SetRemoteFileCachePolicy(string value, string setter) { try { this.RemoteFileCachePolicy = (RequestCacheLevel)Enum.Parse(typeof(RequestCacheLevel), value); executor.Invoke(setter + ": Setting `" + REMOTE_CACHE_POLICY_COMMAND + "` to `" + value + "`."); return(true); } catch { } string[] expected = Enum.GetNames(typeof(RequestCacheLevel)); executor.Invoke(setter + ": Ignoring parameter `" + REMOTE_CACHE_POLICY_COMMAND + "`. Reason: unrecognized value `" + value + "`" + separator + "\tExpected one of the following: " + string.Join(", ", expected)); return(false); }
public static Image GetImage(string url, RequestCacheLevel cache, int id = 0) { try { return(Image.FromStream(GetRemoteImageStream(url, id))); } catch { if (!ignoreWarning.Contains(id)) { Program.Logger.Write(Rallion.LoggerLevel.Warning, string.Format(GlobalStrings.Utility_GetImage, url)); } } return(null); }
public static Image GetImage(string url, RequestCacheLevel cache, int id = 0) { try { WebClient wc = new WebClient(); wc.CachePolicy = new RequestCachePolicy(cache); byte[] bytes = wc.DownloadData(url); MemoryStream ms = new MemoryStream(bytes); return(Image.FromStream(ms)); } catch { if (!ignoreWarning.Contains(id)) { Program.Logger.Write(Rallion.LoggerLevel.Warning, string.Format(GlobalStrings.Utility_GetImage, url)); } } return(null); }
private void GetChannelDataFiles(ChannelSubscriptions channelSubscriptions, ref bool bCancelled, WebClient client) { ReportProgress(0, 10, "Updating your subscriptions' playlist"); _logger.WriteTimestampedMessage("Getting channel data files"); int noSubscriptions = channelSubscriptions.SubscriptionSet.Count; float step = 100F / (float)noSubscriptions; float count = 0; RequestCacheLevel cacheLevel = _verboseMode ? RequestCacheLevel.Revalidate : RequestCacheLevel.Default; client.CachePolicy = new RequestCachePolicy(cacheLevel); // get channel data for each channel subscription foreach (ChannelSubscription channelSubscription in channelSubscriptions.SubscriptionSet) { if (_worker != null && _worker.CancellationPending) { bCancelled = true; return; } ReportProgress(count * step, 10, "Updating playlist for Stream: " + channelSubscription.ChannelName); // checksum of channel file string channelFileFullPath = _channelDataPath + channelSubscription.ChannelID + "_channel.dat"; try { _logger.WriteTimestampedMessage("Attempting to get Channel " + channelSubscription.ChannelID); client.DownloadFile(_cdnSubdomain + "channel/" + channelSubscription.ChannelID + "_channel.dat", channelFileFullPath); } catch (Exception ex) { // write error but do not return. continue with the other channels. _logger.WriteError(ex); } } ReportProgress(100, 20); }
internal static async Task <WebReply> DownloadHtmlAsync(string urlAddress, RequestCacheLevel requestCacheLevel) { var request = (HttpWebRequest)WebRequest.Create(urlAddress); request.CachePolicy = new RequestCachePolicy(requestCacheLevel); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { var receiveStream = response.GetResponseStream(); try { StreamReader readStream; if (string.IsNullOrWhiteSpace(response.CharacterSet)) { readStream = new StreamReader(receiveStream); } else { readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet)); } var data = readStream.ReadToEnd(); response.Close(); readStream.Close(); return(new WebReply(data, HttpStatusCode.OK)); } catch { return(new WebReply(null, HttpStatusCode.ExpectationFailed)); } } return(new WebReply(null, response.StatusCode)); }
public override WebResponse GetResponse() { bool cachedPackageAvailable = IsCachedPackage; // if there is no cached package or it is from the public PackageStore, we must respect CachePolicy if (!cachedPackageAvailable || (cachedPackageAvailable && _respectCachePolicy)) { // inspect and act on CachePolicy RequestCacheLevel policy = _cachePolicy.Level; if (policy == RequestCacheLevel.Default) { policy = _defaultCachePolicy.Level; } switch (policy) { case RequestCacheLevel.BypassCache: { // ignore cache entry cachedPackageAvailable = false; } break; case RequestCacheLevel.CacheOnly: { // only use cached value if (!cachedPackageAvailable) { throw new WebException(SR.Get(SRID.ResourceNotFoundUnderCacheOnlyPolicy)); } } break; case RequestCacheLevel.CacheIfAvailable: { // use cached value if possible - we need take no explicit action here } break; default: { throw new WebException(SR.Get(SRID.PackWebRequestCachePolicyIllegal)); } } } if (cachedPackageAvailable) { #if DEBUG if (PackWebRequestFactory._traceSwitch.Enabled) { System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebRequest - Getting response from Package Cache"); } #endif return(new PackWebResponse(_uri, _innerUri, _partName, _cacheEntry, _cachedPackageIsThreadSafe)); } else { // only return a real WebRequest instance - throw on a PseudoWebRequest WebRequest request = GetRequest(false); if (_webRequest == null || _webRequest is PseudoWebRequest) { throw new InvalidOperationException(SR.Get(SRID.SchemaInvalidForTransport)); } #if DEBUG if (PackWebRequestFactory._traceSwitch.Enabled) { System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebRequest - Getting new response"); } #endif // Create a new response for every call return(new PackWebResponse(_uri, _innerUri, _partName, request)); } }
public RequestCachePolicy(RequestCacheLevel level) { this.level = level; }
public static bool TryLoadTaxonomy( string taxonomyPath, RequestCacheLevel remoteFileCachePolicy, //XmlCatalogResolver xmlCatalog, out Taxonomy currentTaxonomy, out int numberErrors, out string errorMsg) { errorMsg = string.Empty; currentTaxonomy = new Taxonomy(); currentTaxonomy.PromptUser = false; currentTaxonomy.CachePolicy = remoteFileCachePolicy; //currentTaxonomy.XmlCatalog = xmlCatalog; try { numberErrors = currentTaxonomy.Load(taxonomyPath, false); if (numberErrors == 0) { currentTaxonomy.Parse(out numberErrors); } } catch (XPathException) { numberErrors = 1; errorMsg = "Error parsing the taxonomy: Unable to find one or more of the dependent taxonomy files for taxonomy " + taxonomyPath; return false; } // ignore calc linkbase errors - don'care if (numberErrors != 0 && currentTaxonomy.ErrorList.Count > 0) { Console.WriteLine( " Pres Errors: " + currentTaxonomy.NumPresErrors ); Console.WriteLine( " Calc Errors: " + currentTaxonomy.NumCalcErrors ); Console.WriteLine( " Label Errors: " + currentTaxonomy.NumLabelErrors ); Console.WriteLine( " Reference Errors: " + currentTaxonomy.NumReferenceErrors ); currentTaxonomy.ErrorList.Sort(); try { foreach ( ParserMessage pm in currentTaxonomy.ErrorList ) { if ( pm.Level != TraceLevel.Error ) { break; // all the errors should be first after sort } errorMsg += pm.Message + Environment.NewLine; Console.WriteLine( pm.Level.ToString() + ": " + pm.Message ); } errorMsg = "Error parsing the taxonomy: "+ errorMsg.Trim(); } //Do nothing. Error wasn't written to the event log. catch { } // don't care about calc errors - if it's anything else, bomb out if ( numberErrors != currentTaxonomy.NumCalcErrors ) { return false; } } return true; }
// flag below because it's a *pain* to break inside this routine unless it was intended. // we want the exceptions from here to considered the root for the debugger to land. /// <summary> /// Executes a JSON based REST rpc call /// </summary> /// <param name="baseUrl">The url to use</param> /// <param name="args">Arguments to append to the url, represented as a simplified json object (only holds scalar values)</param> /// <param name="payloadJson">The JSON to send as part of the request body</param> /// <param name="timestampField">If specificed, indicates a field to insert a timestamp into. This timestamp represents the time the object was last fetched (either from the file cache or from the server)</param> /// <param name="httpMethod">Set this to use a custom HTTP method (like DELETE)</param> /// <param name="fixupResponse">An optional custom routine that can massage the JSON returned into a more acceptable form. This is useful if the data from the server needs to be adjusted prior to anything else.</param> /// <param name="fixupError">An optional custom routine that can massage the JSON error object returned into a more acceptable form. This is useful if the error from the server needs to be adjusted prior to anything else. You may need to specify this to get the <see cref="JsonRpcException" /> to work properly.</param> /// <param name="cache">Indicates whether or not a file cache should be used for requests. The in memory cache is only on a per-thread basis. The file cache stores by URL and is global. The cache is very aggressive.</param> /// <returns>The JSON returned in the response body.</returns> /// <remarks>Exceptions are expected to have JSON in the response body. This JSON is used as the data for the exception. The exception thrown currently uses a simple heuristic to figure out what the relevant fields are. Usually however, you'll get the data using the Json property of it.</remarks> //[DebuggerNonUserCode] public static object Invoke( string baseUrl, IDictionary <string, object> args = null, object payloadJson = null, string timestampField = null, string httpMethod = null, Func <object, object> fixupResponse = null, Func <object, object> fixupError = null, JsonRpcCacheLevel cache = JsonRpcCacheLevel.Conservative) { HttpWebRequest wreq = null; HttpWebResponse wrsp = null; var url = GetInvocationUrl(baseUrl, args); wreq = WebRequest.Create(url) as HttpWebRequest; wreq.KeepAlive = true; wreq.Pipelined = true; wreq.AllowAutoRedirect = true; RequestCacheLevel cp = RequestCacheLevel.Default; switch (cache) { case JsonRpcCacheLevel.None: cp = RequestCacheLevel.NoCacheNoStore; break; case JsonRpcCacheLevel.Conservative: cp = RequestCacheLevel.Revalidate; break; case JsonRpcCacheLevel.Aggressive: cp = RequestCacheLevel.CacheIfAvailable; break; } wreq.CachePolicy = new RequestCachePolicy(cp); if (null != payloadJson) { if (string.IsNullOrEmpty(httpMethod)) { wreq.Method = "POST"; } else { wreq.Method = httpMethod; } wreq.ContentType = "application/json"; using (var sw = new StreamWriter(wreq.GetRequestStream())) { JsonObject.WriteTo(payloadJson, sw, null); sw.Flush(); } } else if (string.IsNullOrEmpty(httpMethod)) { wreq.Method = "GET"; } else { wreq.Method = httpMethod; } try { wrsp = wreq.GetResponse() as HttpWebResponse; if (wrsp.StatusCode != HttpStatusCode.NoContent) { using (var reader = JsonTextReader.CreateFrom(new StreamReader(wrsp.GetResponseStream()))) { object data = null; try { data = reader.ParseSubtree(); } catch (ExpectingException eex) { var ex = new JsonObject(); ex.Add("status_code", -39); ex.Add("status_message", "Malformed or empty JSON data returned: " + eex.Message); ex.Add("http_status", (int)wrsp.StatusCode); ex.Add("http_status_message", wrsp.StatusDescription); throw new JsonRpcException(ex); } if (null != fixupResponse) { data = fixupResponse(data); } if (!string.IsNullOrEmpty(timestampField)) { var d = data as IDictionary <string, object>; if (null != d) { DateTime dt = DateTime.UtcNow; d.Add(timestampField, dt.ToString("O")); } } return(data); } } else { return(null); } } catch (Exception ex) { var wex = ex as WebException; if (null != wex) { if (null == wrsp) { wrsp = wex.Response as HttpWebResponse; } if (null != wrsp) { using (var reader = JsonTextReader.CreateFrom(new StreamReader(wrsp.GetResponseStream()))) { object data = null; try { data = reader.ParseSubtree(); } catch (ExpectingException eex) { var jex = new JsonObject(); jex.Add("status_code", -39); jex.Add("status_message", "Malformed or empty JSON data returned: " + eex.Message); jex.Add("http_status", (int)wrsp.StatusCode); jex.Add("http_status_message", wrsp.StatusDescription); data = jex; } if (null != fixupError) { data = fixupError(data); } var d = data as IDictionary <string, object>; if (!string.IsNullOrEmpty(timestampField)) { if (null != d) { d.Add(timestampField, DateTime.UtcNow.ToString("O")); } } if (null != d) { throw new JsonRpcException(d); } } } } throw; } }
/// <summary>Initializes a new instance of the <see cref="T:System.Net.Cache.RequestCachePolicy" /> class. using the specified cache policy.</summary> /// <param name="level">A <see cref="T:System.Net.Cache.RequestCacheLevel" /> that specifies the cache behavior for resources obtained using <see cref="T:System.Net.WebRequest" /> objects. </param> /// <exception cref="T:System.ArgumentOutOfRangeException">level is not a valid <see cref="T:System.Net.Cache.RequestCacheLevel" />.value.</exception> public RequestCachePolicyAdapter(RequestCacheLevel level) : this(new RequestCachePolicy(level)) { }
/// <summary> /// Creates a new instance of the System.Net.Cache.RequestCachePolicy class. /// </summary> public RequestCachePolicy() { _level = RequestCacheLevel.Default; }
public void CachePolicy(RequestCacheLevel value) => Test(new RequestCachePolicy(value), HttpWebRequestBuilder.CachePolicy, request => request.CachePolicy);
public RequestCachePolicy(RequestCacheLevel level) { }
/// <summary> /// HTTP 请求数据. /// </summary> /// <param name="requestUrl">请求url地址</param> /// <param name="requestMethod">请求方式</param> /// <param name="requestHeader">请求头部参数</param> /// <param name="requestBody">请求内容</param> /// <param name="timeout">请求超时时间</param> /// <param name="requestEncoding">请求时所用的编码</param> /// <param name="requestCacheLevel">请求的缓存级别</param> /// <param name="contentType">请求内容类型</param> /// <param name="refererUrl">客户端请求地址url</param> /// <param name="accept">客户端可以接受任何数据类型</param> /// <param name="userAgent">客户端浏览器类型</param> /// <param name="host">客户端主机:端口</param> /// <param name="cookies">cookies</param> /// <returns></returns> public static string HttpRequest(string requestUrl, string requestMethod, string requestHeader, string requestBody, int?timeout, string contentType, string refererUrl, string accept, string userAgent, string host, CookieCollection cookies, Encoding requestEncoding, RequestCacheLevel requestCacheLevel = RequestCacheLevel.Default) { AssertUtil.NotNullOrWhiteSpace(requestUrl, LanguageUtil.Translate("com_HttpHelper_HttpRequest_check_url")); HttpWebRequest request = null; try { request = (HttpWebRequest)WebRequest.Create(requestUrl); request.Method = requestMethod; request.Accept = string.IsNullOrEmpty(accept) ? DefaultAccept : accept; request.UserAgent = string.IsNullOrEmpty(userAgent) ? DefaultUserAgent : userAgent; request.ContentType = string.IsNullOrEmpty(contentType) ? DefaultContentType : contentType; request.Timeout = timeout.HasValue ? timeout.Value : 100000; request.CachePolicy = new RequestCachePolicy(requestCacheLevel); if (!string.IsNullOrEmpty(refererUrl)) { request.Referer = refererUrl; } if (!string.IsNullOrEmpty(host)) { request.Host = host; } if (!string.IsNullOrEmpty(requestHeader)) { if (requestHeader.IndexOf('&') != -1) { var compareArr = requestHeader.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); if (compareArr.Length > 0) { foreach (var s in compareArr) { if (s.IndexOf('=') != -1) { var dic = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (dic.Length == 2) { request.Headers.Add(dic[0], dic[1]); } } } } } } if (!request.Headers.AllKeys.Contains("Accept-Encoding")) { request.Headers.Add("Accept-Encoding", DefaultAcceptEncoding); } if (!request.Headers.AllKeys.Contains("Accept-Language")) { request.Headers.Add("Accept-Language", DefaultAcceptLanguage); } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } if ("post" == requestMethod.Trim().ToLower()) { byte[] data = requestEncoding.GetBytes(requestBody); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } return(ResponseBody(request)); } catch (HttpException httpException) { var responseModel = new ResponseModel() { StatusCode = httpException.GetHttpCode(), Success = false, Body = httpException.GetHtmlErrorMessage() }; return(responseModel.ToJSON()); } catch (Exception ex) { throw ex; } finally { request.Abort(); request = null; } }
public static bool TryLoadTaxonomy(string taxonomyPath, RequestCacheLevel remoteFileCachePolicy, //XmlCatalogResolver xmlCatalog, out Taxonomy currentTaxonomy, out int numberErrors, out string errorMsg) { errorMsg = string.Empty; currentTaxonomy = new Taxonomy(); currentTaxonomy.PromptUser = false; currentTaxonomy.CachePolicy = remoteFileCachePolicy; //currentTaxonomy.XmlCatalog = xmlCatalog; try { numberErrors = currentTaxonomy.Load(taxonomyPath, false); if (numberErrors == 0) { currentTaxonomy.Parse(out numberErrors); } } catch (XPathException) { numberErrors = 1; errorMsg = "Error parsing the taxonomy: Unable to find one or more of the dependent taxonomy files for taxonomy " + taxonomyPath; return(false); } // ignore calc linkbase errors - don'care if (numberErrors != 0 && currentTaxonomy.ErrorList.Count > 0) { Console.WriteLine(" Pres Errors: " + currentTaxonomy.NumPresErrors); Console.WriteLine(" Calc Errors: " + currentTaxonomy.NumCalcErrors); Console.WriteLine(" Label Errors: " + currentTaxonomy.NumLabelErrors); Console.WriteLine(" Reference Errors: " + currentTaxonomy.NumReferenceErrors); currentTaxonomy.ErrorList.Sort(); try { foreach (ParserMessage pm in currentTaxonomy.ErrorList) { if (pm.Level != TraceLevel.Error) { break; // all the errors should be first after sort } errorMsg += pm.Message + Environment.NewLine; Console.WriteLine(pm.Level.ToString() + ": " + pm.Message); } errorMsg = "Error parsing the taxonomy: " + errorMsg.Trim(); } //Do nothing. Error wasn't written to the event log. catch { } // don't care about calc errors - if it's anything else, bomb out if (numberErrors != currentTaxonomy.NumCalcErrors) { return(false); } } return(true); }
/// <summary> /// Creates a new instance of the System.Net.Cache.RequestCachePolicy class using the specified cache policy. /// </summary> /// <param name="level">The requested cache level</param> public RequestCachePolicy(RequestCacheLevel level) { _level = level; }
public RequestCachePolicy (RequestCacheLevel level) { this.level = level; }
public RequestCachePolicy(RequestCacheLevel level) { throw new NotImplementedException(); }