Your programs wont use the cache but you can enable

Info icon This preview shows pages 544–546. Sign up to view the full content.

View Full Document Right Arrow Icon
your programs won’t use the cache, but you can enable caching by setting the request’s CachePolicy , as Example 13-20 shows. 520 | Chapter 13: Networking
Image of page 544

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 13-20. Setting cache policy HttpRequestCachePolicy cachePolicy = new HttpRequestCachePolicy( HttpRequestCacheLevel.CacheIfAvailable); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(" "); request.CachePolicy = cachePolicy; The default policy is BypassCache , which means that not only will requests not look in the cache, but any resources you fetch will not be added to the cache. Exam- ple 13-20 , on the other hand, will use a cached copy of the resource if one is available, and if not, it will add the resource it downloads to the cache (unless headers in the HTTP response indicate that it’s not a cacheable resource). The HttpRequestCacheLevel enumeration supports various other caching options. If you want to force the resource to be fetched anew, but would like the result to be added to the cache, you can specify Reload . You can also force a check for freshness—HTTP allows clients to tell the server that they have a cached version and that they want to download the resource only if a newer version is available, and you can enable this behavior with Revalidate . (Some more obscure options are also available, for devel- opers who are familiar with the full complexities of HTTP caching and want complete control.) Using cookies As far as the HTTP specification is concerned, each request is entirely unconnected with any previous requests from the same client. But it’s often useful for a website to be able to recognize a series of requests as having come from the same client, and so a common mechanism to support this, called cookies, is widely used. * Cookies underpin features such as shopping baskets, where a web application needs to maintain per-user state—I expect to see only the things that I’ve put in my basket, and not the items that any other users who are logged in right now have put in theirs. Cookies are also com- monly used for managing logins—once the user has typed in his username and pass- word in an HTML form, a cookie is often used, in effect, to authenticate the user from then on. If you’re using a web browser, cookies just work without needing any intervention (unless you’ve disabled them, of course). But if you’re writing code, you need to take specific steps to use them—by default, .NET will not use cookies at all, and does not have access to the cookie store for Internet Explorer. Nor does it implement a cookie store of its own. * Cookies are so widely supported that although they’re not technically part of the HTTP specification, they might as well be. † Silverlight applications are an exception. They rely on the web browser to make HTTP requests, and so your requests will send whatever cookies the containing browser would normally send.
Image of page 545
Image of page 546
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern