The original HTTP design became stateless, request/response-oriented, and did not prepare for stateful sessions that spanned multiple logically dependent request/response exchanges, with the HTTP protocol being used more and more widely, Many systems have been using it for services beyond the expected application, such as e-commerce applications, so state management has become increasingly necessary.
Netscape was the leader in Web client and server software development, and implemented HTTP state management in its products based on private specifications, and then Netscape tried to standardize the mechanism by publishing draft specifications that helped to form normative documents through RFCs, However, in most applications, state management is still largely based on the draft of Netscape and is incompatible with official specifications, and web developers can feel that forced retention of these applications has led to the fragmentation of the standard specification.
3.1 HTTP Cookies
An HTTP cookie is a small piece of state information about the interaction between the HTTP client and server, or a token used to maintain a session message, and the name is preserved by a Netscape engineer using a magic cookie to describe it.
HttpClient uses a cookie interface to represent an abstract cookie tag, the simplest form of an HTTP cookie is a key-value pair, which typically contains a set of properties such as a legal domain name, a subset of URLs that a cookie can receive, The maximum time that a cookie is saved.
The Setcookie interface represents the Set-cookie response header message, which is sent from the server side to the client to maintain a session state.
The Clientcookie interface inherits from the cookie interface, which enables additional client-specific functionality such as accurate retrieval of the source cookie properties specified by the source server, which is important for generating cookie header information, Because some cookie specifications require specific properties to be included in the cookie header only if they are specified in the Set-cookie header message.
The following is an example of creating a client cookie object:
Basicclientcookie cookie = new Basicclientcookie ("name", "value");
Set effective domain and path attributes
Cookie.setdomain (". mycompany.com");
Cookie.setpath ("/");
Set attributes exactly as sent by the server
Cookie.setattribute (clientcookie.path_attr, "/");
Cookie.setattribute (Clientcookie.domain_attr, ". mycompany.com");
3.2 Cookie Specification
The Cookiespec interface represents the specification of cookie management, and the cookie Management specification includes the following:
Parsing the rules of Set-cookie head
To verify the rules for resolved cookies
Formats the cookie head for a given host, port, and source path
HttpClient comes with a variety of COOKIESPEC implementations:
Standard Strict (strict): state Management policy behavior fully conforms to the RFC6265 fourth chapter of the behavioral definition.
Standard (Standard): A State management strategy is more in line with the behavior defined in chapter Fourth of RFC6265 to expect interaction between servers that are not fully compliant with the behavior.
Netscape Draft (OBSOLETE): The strategy adheres to Netscape's initial draft specification, and avoids using it unless it does need to be compatible with old code.
RFC 2965 (OBSOLETE): The State management policy conforms to the outdated RFC2965 defined state management specification. Please do not use in new applications.
RFC2109 (OBSOLETE): The State management policy conforms to the outdated RFC2109 defined state management specification, and should not be used in new applications.
browser compatibility browser compatibility (OBSOLETE): This strategy tries to simulate old browser versions such as Microsoft IE and Mozilla FireFox, please do not use them in new applications.
Default Cookie policy is a comprehensive policy that is based on the HTTP response returned cookie properties such as version information, expiration information, and compatibility with rfc2965,rfc2109 or Netscape drafts. This policy will be discarded in the next httpclient iteration (based on RFC6265).
Ignore Cookies: All cookies are ignored
It is strongly recommended that standard or standard strict policies be used in new applications, and that obsolete specifications should be used only when compatible with legacy systems. The next HttpClient version will stop support for obsolete specifications.
3.3 Select Cookie Policy
Cookie policies can be set through HTTP clients and can be overwritten at HTTP request.
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 Custom Cookie policy
To implement a custom cookie policy, you should create a custom Cookiespec interface implementation, create a Cookiespecprovider implementation class, and then use the implementation class to create and initialize an instance of the custom specification, and then register with HttpClient , once the custom specification is registered, it will be triggered just like the standard cookie specification.
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 work in conjunction with any actual cookie memory that implements the Cookiestore interface, and the simulated Cookiestore implementation is called Basiccookiestore, is a simple implementation based on java.util.ArrayList, when the container is garbage collected, if the Basicclientcookie object is recycled, its stored cookies will be lost, you can provide more complex implementation to meet your needs.
Create a local instance of cookie store
cookiestore cookiestore = new Basiccookiestore ();
Populate cookies if needed
basicclientcookie cookies = new Basicclientcookie ("name", "value");
Cookie.setdomain (". mycompany.com");
Cookie.setpath ("/");
Cookiestore.addcookie (cookie);
Set the store
closeablehttpclient httpclient = Httpclients.custom ()
. Setdefaultcookiestore (Cookiestore)
. Build ();
3.6 http State management and run contexts
During HTTP request execution, HttpClient adds the following state management-related objects to the run context.
The Lookup instance represents the actual cookie registration specification, and the value of the property has a priority that is greater than the default context for the current context.
The Cookiespec instance represents the actual cookie specification.
The Cookieorigin instance represents the detailed cookie information for the source server.
The Cookiestore instance represents the actual cookie store, and the value of the property has the priority of the current context greater than the default context.
The current HttpContext object can be used to customize the HTTP State admin context before the request is executed, or to check its status after the request is executed, and you can also implement individual state management through a separate run context, at the HTTP client level, The Ookie registration specification and cookie storage priority for the current context is greater than the default context.
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);
<...>
//Cookies Origin details
cookieorigin cookieorigin = Context.getcookieorigin ();
Cookie Spec used
cookiespec Cookiespec = Context.getcookiespec ();