本文整理汇总了C#中System.Net.Cache.HttpRequestCachePolicy类的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestCachePolicy类的具体用法?C# HttpRequestCachePolicy怎么用?C# HttpRequestCachePolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpRequestCachePolicy类属于System.Net.Cache命名空间,在下文中一共展示了HttpRequestCachePolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateRequest
private static HttpWebRequest CreateRequest(string url, string method)
{
int timeout = Convert.ToInt16(ConfigurationManager.AppSettings["timeout"]);
string userAgent = ConfigurationManager.AppSettings["userAgent"];
try
{
var uri = new Uri(url);
var request = (HttpWebRequest)WebRequest.Create(uri);
if (method != "GET")
{
WebRequest.DefaultWebProxy = new WebProxy();
request.Timeout = timeout;
request.UserAgent = userAgent;
var policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
var noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
}
return request;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return null;
}
}
示例2: Ctor_ExpectedPropertyValues
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());
}
示例3: CreateWebApiRequest
private HttpWebRequest CreateWebApiRequest(String relativeUri)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri(_uri, relativeUri));
request.ContentType = SchedulerConstants.RequestJsonContentType;
request.Headers.Add("Authorization",
new Signature(this._subscriptionId, this._secretKey).ToString());
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
return request;
}
示例4: Test_WebRequest_03
public static void Test_WebRequest_03(string url)
{
// create HttpWebRequest or FileWebRequest
WebRequest req = WebRequest.Create(url);
HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
req.CachePolicy = cachePolicy;
// RequestCachePolicy
_tr.WriteLine("Test_WebRequest_03");
_tr.WriteLine(" url : \"{0}\"", url);
_tr.WriteLine(" WebRequest.Create(url) : \"{0}\"", req);
}
示例5: GetResponse
public string GetResponse(string host, IDictionary<string, object> arguments,
bool useCookies = false)
{
const string contentType = "application/x-www-form-urlencoded";
const string requestType = "POST";
Logging.LogLine("POSTing to " + host + " " + arguments.GetHashCode(), "browser serv");
var isFirst = true;
if (useCookies)
{
if (!arguments.ContainsKey("csrf_token"))
arguments["csrf_token"] = GetCsrfToken();
}
var totalRequest = new StringBuilder();
foreach (var arg in arguments.Where(arg => arg.Key != "type"))
{
if (!isFirst)
totalRequest.Append('&');
else
isFirst = false;
totalRequest.Append(arg.Key);
totalRequest.Append('=');
totalRequest.Append(HttpUtility.UrlEncode((string) arg.Value));
}
var toPost = Encoding.ASCII.GetBytes(totalRequest.ToString());
var cachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
var req = (HttpWebRequest) WebRequest.Create(host);
req.CachePolicy = cachePolicy;
req.Method = requestType;
req.ContentType = contentType;
req.ContentLength = toPost.Length;
req.UserAgent = Constants.FriendlyName;
if (useCookies)
req.CookieContainer = loginCookies;
using (var postStream = req.GetRequestStream())
postStream.Write(toPost, 0, toPost.Length);
using (var rep = (HttpWebResponse) req.GetResponse())
using (var answerStream = rep.GetResponseStream())
{
if (answerStream == null)
return null;
using (var answerReader = new StreamReader(answerStream))
return answerReader.ReadToEnd();
}
}
示例6: GetResponseJSON
// retornar la respuesta en JSON.
private static string GetResponseJSON(string uri)
{
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
WebRequest request = WebRequest.Create(uri);
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
示例7: Get
/// <summary>
/// Gets web response of request specifying accept header header explicitly
/// </summary>
/// <param name="request">HttpWebRequest request</param>
/// <param name="acceptHeaderValue">The specified accept header</param>
/// <param name="maximumPayloadSize">Maximum size of response in byte to be allowed to receive</param>
/// <returns>Web response</returns>
public static Response Get(HttpWebRequest request, string acceptHeaderValue, int maximumPayloadSize)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (!string.IsNullOrEmpty(acceptHeaderValue))
{
request.Accept = acceptHeaderValue;
}
// make sure cache disabled, otherwise intermediate proxy would likely return cached responses
HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = cachePolicy;
return WebHelper.Get(request, maximumPayloadSize);
}
示例8: GetResponse
public string GetResponse(string host, IEnumerable<KeyValuePair<string, object>> arguments,
bool useCookies = false)
{
const string contentType = "application/x-www-form-urlencoded";
const string requestType = "POST";
var isFirst = true;
var totalRequest = new StringBuilder();
foreach (var arg in arguments.Where(arg => arg.Key != "type"))
{
if (!isFirst)
totalRequest.Append('&');
else
isFirst = false;
totalRequest.Append(arg.Key);
totalRequest.Append('=');
totalRequest.Append(HttpUtility.UrlEncode((string) arg.Value));
}
var toPost = Encoding.ASCII.GetBytes(totalRequest.ToString());
var cachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
var req = (HttpWebRequest) WebRequest.Create(host);
req.CachePolicy = cachePolicy;
req.Method = requestType;
req.ContentType = contentType;
req.ContentLength = toPost.Length;
if (useCookies)
req.CookieContainer = loginCookies;
using (var postStream = req.GetRequestStream())
postStream.Write(toPost, 0, toPost.Length);
using (var rep = (HttpWebResponse) req.GetResponse())
using (var answerStream = rep.GetResponseStream())
{
if (answerStream == null)
return null;
using (var answerReader = new StreamReader(answerStream))
return answerReader.ReadToEnd(); // read our response
}
}
示例9: TestCache
public void TestCache()
{
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
Session.CachePolicy = policy;
Request req = new Request(Session, new Uri("http://www.dr.dk/drfront/images//2013/05/21/c=0,17,1024,505;w=300;42676.jpg"));
using (var resp = req.Accept("image/jpeg").Get<byte[]>())
{
resp.SaveToFile("c:\\tmp\\scrap.jpg");
Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);
}
using (var resp = req.Accept("image/jpeg").Get<byte[]>())
{
resp.SaveToFile("c:\\tmp\\scrap.jpg");
Assert.AreEqual(HttpStatusCode.NotModified, resp.StatusCode);
}
}
示例10: GetRequestModel
/// <summary>
/// 请求并返回
/// </summary>
public static string GetRequestModel()
{
var url = string.Format("http://192.168.1.180:8002/login.aspx?key={0}", "test");
HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "GET";
HttpRequestCachePolicy cPolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
webRequest.CachePolicy = cPolicy;
webRequest.Pipelined = false;
webRequest.KeepAlive = false;
webRequest.AllowAutoRedirect = true;
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webRequest.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
webRequest.Headers.Add("Cookie", "JSESSIONID=396C8439833102EFAF868D6297A9D8FD.web25_8081; __utma=142379556.1006189020.1343613376.1343613376.1343651790.2; __utmb=142379556.2.10.1343651790; __utmc=142379556; __utmz=142379556.1343613376.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)");
HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
var strRet = string.Empty;
try
{
System.IO.Stream resStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.Default;
StreamReader readStream = new StreamReader(resStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
strRet = strRet + str;
count = readStream.Read(read, 0, 256);
}
resStream.Close();
}
catch (Exception e)
{
strRet = "";
}
return strRet;
}
示例11: RequestCachingSectionInternal
internal RequestCachingSectionInternal(RequestCachingSection section)
{
if (!section.DisableAllCaching)
{
this.defaultCachePolicy = new RequestCachePolicy(section.DefaultPolicyLevel);
this.isPrivateCache = section.IsPrivateCache;
this.unspecifiedMaximumAge = section.UnspecifiedMaximumAge;
}
else
{
this.disableAllCaching = true;
}
this.httpRequestCacheValidator = new HttpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
this.ftpRequestCacheValidator = new FtpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
this.defaultCache = new WinInetCache(this.IsPrivateCache, true, true);
if (!section.DisableAllCaching)
{
HttpCachePolicyElement defaultHttpCachePolicy = section.DefaultHttpCachePolicy;
if (defaultHttpCachePolicy.WasReadFromConfig)
{
if (defaultHttpCachePolicy.PolicyLevel == HttpRequestCacheLevel.Default)
{
HttpCacheAgeControl cacheAgeControl = (defaultHttpCachePolicy.MinimumFresh != TimeSpan.MinValue) ? HttpCacheAgeControl.MaxAgeAndMinFresh : HttpCacheAgeControl.MaxAgeAndMaxStale;
this.defaultHttpCachePolicy = new HttpRequestCachePolicy(cacheAgeControl, defaultHttpCachePolicy.MaximumAge, (defaultHttpCachePolicy.MinimumFresh != TimeSpan.MinValue) ? defaultHttpCachePolicy.MinimumFresh : defaultHttpCachePolicy.MaximumStale);
}
else
{
this.defaultHttpCachePolicy = new HttpRequestCachePolicy(defaultHttpCachePolicy.PolicyLevel);
}
}
FtpCachePolicyElement defaultFtpCachePolicy = section.DefaultFtpCachePolicy;
if (defaultFtpCachePolicy.WasReadFromConfig)
{
this.defaultFtpCachePolicy = new RequestCachePolicy(defaultFtpCachePolicy.PolicyLevel);
}
}
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:37,代码来源:RequestCachingSectionInternal.cs
示例12: GetHTML
public WebReqResult GetHTML(string url)
{
WebReqResult result = new WebReqResult();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
WebHeaderCollection requestHeaderCollection = request.Headers;
requestHeaderCollection.Add("Accept-Language:en-US,en;q=0.5");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.CachePolicy = cachePolicy;
request.Proxy = null; // Proxy autodetection slows down requests ( http://stackoverflow.com/a/935728 )
request.Timeout = 5000;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream stream = response.GetResponseStream();
StreamReader readStream = new StreamReader(stream);
string html = WebUtility.HtmlDecode(readStream.ReadToEnd());
result.Success = true;
result.HTML = html;
}
}
catch (Exception e)
{
result.Success = false;
result.Error = e.Message;
}
return result;
}
示例13: CreateRequest
private static HttpWebRequest CreateRequest(Uri url, string data, string method, string contentType, NameValueCollection headers, int timeout, bool gzip, NetworkCredential credential = null)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method.ToUpper();
request.ContentType = contentType;
var noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
request.Timeout = timeout;
request.Credentials = credential;
if (headers != null)
request.Headers.Add(headers);
if (gzip)
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
if (data != null)
{
var encoding = new UTF8Encoding();
var byteData = encoding.GetBytes(data);
request.ContentLength = byteData.Length;
using (var postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
}
else if (request.Method != "GET")
{
//Specify a zero content length to prevent 411s
request.ContentLength = 0;
}
return request;
}
示例14: SetCacheControlWithMaxAgeAndMinFresh
public void SetCacheControlWithMaxAgeAndMinFresh(TimeSpan maxAge, TimeSpan minFresh)
{
_cachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, maxAge, minFresh);
}
示例15: SetCacheControlWithMaxAgeAndMaxStale
public void SetCacheControlWithMaxAgeAndMaxStale(TimeSpan maxAge, TimeSpan maxStale)
{
_cachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMaxStale, maxAge, maxStale);
}