HttpClient Chapter III HTTP State management

Source: Internet
Author: User
Tags deprecated http cookie

The original HTTP is set to a stateless response-oriented protocol, and it does not provide the required functionality for stateful sessions based on request/response interchanges that span several logically related requests. But as the HTTP protocol becomes more and more popular and applied, more and more systems are starting to use it as a function that is not its function, such as e-commerce transmission applications, so that support for state management becomes a necessary function.

At that time Netscape, as a leading developer of Web clients and server-side software, implemented support for HTTP status management in one of their products based on a special description, and later Netscape tried to standardize the mechanism by releasing a guidebook. These efforts contributed to the adoption of a formal definition of RFC standards. However, the management of a large number of applications is still based on Netscape's instructions and is incompatible with the official definition. Developers of all major browsers feel the need to be compatible with those that promote standards.

3.1 HTTP Cookies

An HTTP cookie is a mark or a packet that can be exchanged between an HTTP proxy and a target server to maintain state information for a session. Netscape engineers are accustomed to calling it "magical cookies".

HttpClient the simplest form of using a cookie interface to describe an abstract cookie,http cookie is only a name/value pair. Usually an HTTP cookie also contains many attributes, such as a valid domain name, a relative path to a URLs, or the maximum effective period of a cookie.

The Setcookie interface describes a set of cookie response headers that are sent by the original server to the HTTP proxy in order to keep the session state.

The Clientcookie interface inherits the cookie interface and adds specific features, such as the ability to return cookies when the original server explicitly proposes to return the original cookie. This is important for generating cookie headers, as some cookie instructions require that the cookie header contain a certain attribute only if the cookie is specifically requested in the cookie collection header.

Here is an example of creating a client cookie object:

Basicclientcookie cookie = new Basicclientcookie ("name", "value");//Set effective domain and path attributescookie.setdo Main (". mycompany.com"); Cookie.setpath ("/");//Set attributes exactly as sent by the Servercookie.setattribute ( Clientcookie.path_attr, "/"); Cookie.setattribute (Clientcookie.domain_attr, ". mycompany.com");

3.2 Cookie Specification

The Cookiespec interface describes the cookie management specification, and the cookie management specification is expected to strengthen:

    • Resolves the rules that set the cookie header.

    • Check the rules for parsing cookies.

    • The cookie header is formatted for the given original host, port, and path.

HttpClient provides several implementations of the Cookiespec:

    • Strict adherence to the standard policy: the State management policy complies with the syntax and morphology defined in chapter Fourth of RFC6265.

    • Standard policy: The State management policy adheres to a more loosely defined syntax and lexical RFC6265 in chapter fourth, and is intended to be a bit of an existing server interaction that does not comply with standard protocols.

    • Strategy planned by Netscape (deprecated): This strategy complies with the original definition of Netscape's release, unless there is an absolute need to avoid it.

    • RFC2965 policy (deprecated): The state management policy adheres to the deprecated RFC2965 defined policies and is not used in new applications.

    • RFC2109 policy (deprecated): The state management policy adheres to the deprecated RFC2109 defined policies and is not used in new applications.

    • Browser compatibility policy (deprecated): This policy and the old version of the browser such as IE and Firefox conflicts, please do not use in new applications.

    • Default policy: The default cookie policy is a composite policy that complies with RFC2965 and RFC2109 and complies with the properties of the cookie sent based on the HTTP response (for example, the version attribute, which is now obsolete) of the Netscape-produced implementation, This policy is obsolete when the next standard httpclient version is released.

    • Policy to ignore cookies: All cookies are ignored.

It is strongly recommended to use standard or strict compliance policies in new applications. Obsolete specifications should only be used in compatible systems. Policies that support obsolete specifications will be removed when the next major version of HttpClient is released.

3.3 Select Cookie Policy

Cookie policies can be set in httpclient and can also be overridden if required by HTTP requests.

Requestconfig GlobalConfig = Requestconfig.custom (). Setcookiespec (Cookiespecs.default). build (); Closeablehttpclient httpclient = Httpclients.custom (). Setdefaultrequestconfig (GlobalConfig). build (); Requestconfig localconfig = requestconfig.copy (GlobalConfig). Setcookiespec (cookiespecs.standard_strict). build (); HttpGet httpget = new HttpGet ("/"); Httpget.setconfig (Localconfig);

3.4 Customizing Cookie Policies

In order to implement a custom cookie policy, you should create a custom Cookiespec interface implementation, create a Cookiespecprovider implementation class to create and initialize an instance of the custom specification , and use HttpClient to register the factory. Once a custom implementation is registered, it can be activated in the same way as the standard cookie implementation.

Publicsuffixmatcher Publicsuffixmatcher = Publicsuffixmatcherloader.getdefault (); registry<cookiespecprovider> r = registrybuilder.<cookiespecprovider>create (). Register ( Cookiespecs.default,new Defaultcookiespecprovider (Publicsuffixmatcher)). Register (cookiespecs.standard,new Rfc6265cookiespecprovider (Publicsuffixmatcher)). Register ("Easy", New Easyspecprovider ()). build (); Requestconfig requestconfig = Requestconfig.custom (). Setcookiespec ("Easy"). Build (); Closeablehttpclient httpclient = Httpclients.custom (). Setdefaultcookiespecregistry (R). Setdefaultrequestconfig ( Requestconfig). build ();

3.5 Cookie Persistence

HttpClient can handle any persistent cookie storage that implements the Cookiestore interface. The default Cookiestore implementation is Basiccookiestore, which is a simple implementation that is stored with ArrayList. When a container object is garbage collected, the cookie stored in the Basicclientcookie object is lost, and the user can provide more complex implementations if needed.

Create a local instance of cookies Storecookiestore Cookiestore = new Basiccookiestore ();//Populate Cookies if NEEDEDBA Sicclientcookie cookie = new Basicclientcookie ("name", "value"), Cookie.setdomain (". mycompany.com"); Cookie.setpath (" /"); Cookiestore.addcookie (cookie);//Set the Storecloseablehttpclient httpclient = Httpclients.custom (). Setdefaultcookiestore (Cookiestore). build ();

3.6 HTTP state management and execution context

This section adds the following state-management-related objects:

    • The lookup instance represents the actual cookie detail record. This property value is set at a higher precedence than the default in the local write-up text.

    • The Cookiespec instance represents the actual cookie details.

    • The Cookieorigin instance represents the actual details of the original server.

    • The Cookiestore instance represents the actual cookie store. This property value is set at a higher precedence than the default in the local write-up text.

The local HttpContext object can be used to customize the HTTP state management context before the request is executed, or to check its status after the request is executed. You can also use a detached execution context to implement per-user (or per-thread) state management. Cookie details defined in the local context record and the cookie storage priority is higher than the default setting level.

Closeablehttpclient httpclient = <...>Lookup<CookieSpecProvider> Cookiespecreg = <...>cookiestore Cookiestore = <...>httpclientcontext context = Httpclientcontext.create (); Context.setcookiespecregistry ( Cookiespecreg); Context.setcookiestore (Cookiestore); HttpGet httpget = new HttpGet ("http://somehost/"); Closeablehttpresponse response1 = Httpclient.execute (httpget, context); <...>//Cookie origin Detailscookieorigin Cookieorigin = Context.getcookieorigin ();//Cookie spec Usedcookiespec Cookiespec = Context.getcookiespec ();


This article is from the "Evan" blog, be sure to keep this source http://wenshengzhu.blog.51cto.com/5151285/1835465

HttpClient Chapter III HTTP State management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.